我想定义一个自定义向量类,该类别使用std :: vector类带有自定义分配器如下:
template <class T>
typedef std::vector<T, MyLib::MyAlloc<T> > my_vector;
然后,当我将其使用为:
时 my_vector<std::string> v;
我的G 2.95.3 Solaris 10上的编译器抱怨说
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用"新"类型别名语法支持这一点:
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> > {};
可以用最初希望使用的那样使用。