String 从叮当声中混淆编译错误
我正在尝试为String 从叮当声中混淆编译错误,string,c++11,gcc,clang++,allocator,String,C++11,Gcc,Clang++,Allocator,我正在尝试为char编写一个自定义分配器,用于std::basic_string。我编写了以下代码,但它无法使用gcc.godbolt.org上的x86clang3.7进行编译-请参阅。另一方面,GCC成功地编译了它 与往常一样,有很多错误消息,其中之一是: error: 'rebind' following the 'template' keyword does not refer to a template 我迄今为止的工作: #include <algorithm> #inc
char
编写一个自定义分配器,用于std::basic_string
。我编写了以下代码,但它无法使用gcc.godbolt.org上的x86clang3.7进行编译-请参阅。另一方面,GCC成功地编译了它
与往常一样,有很多错误消息,其中之一是:
error: 'rebind' following the 'template' keyword does not refer to a template
我迄今为止的工作:
#include <algorithm>
#include <string>
#include <iostream>
#include <type_traits>
#include <cstring>
template <typename Tp>
struct my_allocator {
};
template <>
struct my_allocator<char> {
char arr[30];
typedef char value_type;
my_allocator()
{
std::cout<<"ctor\n";
}
template <class T>
my_allocator(const my_allocator<T>& other)
{
std::cout<<"copy_ctor\n";
std::memcpy(arr,other.arr,30);
}
char* allocate(std::size_t )
{
std::cout<<"allocated\n";
return arr;
}
void deallocate(char* , std::size_t )
{
std::cout<<"freed\n";
}
};
typedef std::basic_string<char,std::char_traits<char>, my_allocator<char> > my_string;
int main()
{
my_string a("hello");
std::cout<<a;
}
#包括
#包括
#包括
#包括
#包括
模板
结构我的分配程序{
};
模板
结构我的分配程序{
char-arr[30];
typedef char value_type;
my_分配器()
{
std::coutWorks在这个clang3.7+x86_64+stdlibc++组合上很好:那里的clang配置错误,它选择了一个太旧的libstdc++。@dyp请更小心地使用您使用的名称,您可能指的是libstdc++或libc++,而不是stdlibc++。@marglisse抱歉,这仅仅是一个输入错误。我指的是libstdc++,如编译器选项所示这是我的coliru链接。