如何为类模板生成别名
How to generate alias for class templates?
本文关键字:别名 更新时间:2023-10-16
我希望MyVector可以选择std::vector或boost::container::vector。如何实现它?我可以使用宏,但我被告知它们不是很安全。谢谢。
#define MyVector std::vector
// #define MyVector boost::container::vector
c++ 11有别名模板。你可以这样做:
template <typename T>
using MyVector = std::vector<T>;
//using MyVector = boost::container::vector<T>;
然后像这样使用:
MyVector<int> x;
在c++ 03中,你可以使用宏或者元函数。
template <typename T>
struct MyVector {
typedef std::vector<T> type;
//typedef boost::container::vector<T> type;
};
// usage is a bit tricky
MyVector<int>::type x;
// ... or when used in a template
typename MyVector<T>::type x;
相关文章:
- 部分定义/别名模板模板参数
- 如何在C++20中创建模板别名的推导指南
- 告诉c++编译器该参数没有别名
- boost::spirit::karma 替代生成器,带有 boost::variant 由字符串和字符串别名组成
- 继承模板类中的类型别名
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 为什么 GCC 在使用类型别名时处理 const reinterpret_cast不同?
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 为什么我们不能重复使用具有不同模板参数的别名模板标识符?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 如何使用类型别名从模板化类中隐藏模板列表
- 模板模板参数和模板别名:编译器错误?
- 使用定义函数模板别名
- 为模板参数包添加别名
- 如何检测类型类型别名?
- C++类型别名,其中值被替换
- C++使用默认模板参数键入别名和转发声明
- 在Qt中注册自定义元类型的别名类型
- 缺少别名模板C++参数列表
- 使用外部定义的模板类型作为模板参数的更通用模板的模板别名