【发布时间】:2018-02-25 21:18:41
【问题描述】:
我在使用 GCC 编译以下程序时遇到问题(我尝试了很多版本,都失败并出现相同的错误)。它在 Clang 中编译得很好:
#include <vector>
struct Tag1
{
static void logAllocation(){}
static void logDeallocation(){}
};
struct Tag2
{
static void logAllocation(){}
static void logDeallocation(){}
};
template<typename Tag, typename T>
struct MyAllocator
{
using value_type = typename std::allocator<T>::value_type;
T* allocate(std::size_t n)
{
Tag::logAllocation();
return std::allocator<T>{}.allocate(n);
}
void deallocate(T* p, std::size_t n)
{
Tag::logDeallocation();
std::allocator<T>{}.deallocate(p, n);
}
};
int main()
{
std::vector<int, MyAllocator<Tag1, int>> vec;
}
问题是 GCC 认为 Tag==int 在 MyAllocator 中,我收到一个错误,即 'logDeallocation' 不是 'int' 的成员。这是 GCC 中的错误吗?当我翻转模板参数 (template<typename T, typename Tag) 并将我的向量声明为 std::vector<int, MyAllocator<int, Tag1>> vec; 时,它会编译。
【问题讨论】:
-
适用于 clang。
-
@BaummitAugen - 什么版本? Clang 3.8 emits a similar error.
-
问题似乎是
_Vector_Base::_Tp_alloc_type解析为MyAllocator<int, int>。这是由typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template rebind<_Tp>::other _Tp_alloc_type;定义的。我不是分配器方面的专家,但可能是您的分配器缺少使rebind按预期工作所需的东西 -
@StoryTeller 我真的不知道我在说什么,宁愿把它留给更熟悉分配器的人
-
将
template< class U > struct rebind { using other = MyAllocator<Tag, U>; };添加到MyAllocator似乎可以正常工作。