我该怎么办typedef
a template class
?就像是:
typedef std::vector myVector; // <--- compiler error
我知道有两种方法:
(1) #define myVector std::vector // not so good
(2) template<typename T>
struct myVector { typedef std::vector<T> type; }; // verbose
C++0x 中有更好的东西吗?
是的。它被称为“别名模板 http://en.cppreference.com/w/cpp/language/type_alias,”这是 C++11 中的一项新功能。
template<typename T>
using MyVector = std::vector<T, MyCustomAllocator<T>>;
用法将完全符合您的预期:
MyVector<int> x; // same as: std::vector<int, MyCustomAllocator<int>>
GCC 从 4.7 开始支持它,Clang 从 3.0 开始支持它,MSVC 从 2013 SP4 开始支持它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)