繁体   English   中英

模板typedef与std :: vector有自定义分配器

[英]template typedef with std::vector to have custom allocator

我想定义一个自定义向量类,它使用带有自定义分配器的std :: vector类,如下所示:

template <class T>
typedef std::vector<T, MyLib::MyAlloc<T> > my_vector;

然后,当我尝试使用它时:

  my_vector<std::string> v;

我在Solaris 10上的g ++ 2.95.3编译器抱怨说

 template declaration of `typedef class vector<T,MyLib::MyAlloc<T1> > my_vector'
aggregate `class my_vector<basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > > v' has incomplete type and cannot be initialized

请帮我纠正一下这个片段。

C ++ 11使用“new”类型别名语法支持此功能:

template <class T>
using my_vector = std::vector<T, MyLib::MyAlloc<T> >;

“旧”表单( typedef )不能用于创建别名模板。


如果C ++ 11或更高版本不是一个选项。 唯一的办法是模板元功能:

template <class T>
struct my_vector {
  typedef std::vector<T, MyLib::MyAlloc<T> > type;
};

哪个可以这样使用:

my_vector<std::string>::type v;

或者,因为std::vector是类类型:

template <class T>
struct my_vector : std::vector<T, MyLib::MyAlloc<T> > {};

可以使用哪个,因为你最初希望它被使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM