問題描述
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 - 你的工作是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!