問(wèn)題描述
C++ 0x 具有模板別名(有時(shí)稱為模板 typedef).請(qǐng)參閱此處.C++ 的當(dāng)前規(guī)范沒(méi)有.
C++ 0x has template aliases (sometimes referred to as template typedefs). See here. Current spec of C++ does not.
你喜歡用什么來(lái)解決?容器對(duì)象還是宏?你覺得值得嗎?
What do you like to use as work around ? Container objects or Macros ? Do you feel its worth it ?
推薦答案
你喜歡用什么來(lái)解決?容器對(duì)象還是宏?你覺得值得嗎?
What do you like to use as work around ? Container objects or Macros ? Do you feel its worth it ?
規(guī)范的方法是使用像這樣的元函數(shù):
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
) 和許多庫(kù),包括 Boost.我們?cè)谏镄畔?kù)中廣泛使用它.
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 在評(píng)論中指出的 Boost/STL 約定.)
( I've amended the code to reflect Boost/STL conventions as pointed out by Daniel in his comment.)
這篇關(guān)于模板 typedefs - 你的工作是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!