Template typedefs - What#39;s your work around?(模板 typedefs - 你的工作是什么?)
问题描述
C++ 0x 具有模板别名(有时称为模板 typedef).请参阅此处.C++ 的当前规范没有.
C++ 0x has template aliases (sometimes referred to as template typedefs). See here. Current spec of C++ does not.
你喜欢用什么来解决?容器对象还是宏?你觉得值得吗?
What do you like to use as work around ? Container objects or Macros ? Do you feel its worth it ?
推荐答案
你喜欢用什么来解决?容器对象还是宏?你觉得值得吗?
What do you like to use as work around ? Container objects or Macros ? Do you feel its worth it ?
规范的方法是使用像这样的元函数:
The canonical way is to use a metafunction like thus:
template <typename T>
struct my_string_map {
typedef std::map<std::string, T> type;
};
// Invoke:
my_string_map<int>::type my_str_int_map;
这也用于 STL (allocator::rebind
) 和许多库,包括 Boost.我们在生物信息库中广泛使用它.
This is also used in the STL (allocator::rebind<U>
) and in many libraries including Boost. We use it extensively in a bioinformatical library.
它很臃肿,但在 99% 的情况下都是最好的选择.在这里使用宏是不值得的.
It's bloated, but it's the best alternative 99% of the time. Using macros here is not worth the many downsides.
(我修改了代码以反映 Daniel 在评论中指出的 Boost/STL 约定.)
( I've amended the code to reflect Boost/STL conventions as pointed out by Daniel in his comment.)
这篇关于模板 typedefs - 你的工作是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模板 typedefs - 你的工作是什么?


基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05