c++ - 如何 typedef 模板类?

标签 c++ templates c++11 typedef

我应该如何 typedef 一个 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 中有什么更好的吗?

最佳答案

是的。它被称为“alias template”,是 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 中支持。

关于c++ - 如何 typedef 模板类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907194/

相关文章:

c# - 实时视频流快速编码

c++ - 如何通过类模板专门化运算符函数类型

c++ - 如何在类声明之外声明模板函数

c++ - 此代码是否会释放为 MULTIMAP 分配的内存?

c++ - 在不同的时间传递不同的参数

c++ - 如何在 cocos2dx v3.7 中将一些 Sprite 放入 Valuemap

c++ - 在 OpenGL 中存储模型矩阵的最佳位置?

C++ char** -> vector<string> -> string -> char** 解析问题

css - 使用希伯来字符的 Infine 模板无法在 IE 中正确显示

c++ - 为什么我在 cin 上出现段错误?