Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

c++ - Template typedefs - What's your work around?

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

This is also used in the STL (allocator::rebind<U>) and in many libraries including Boost. We use it extensively in a bioinformatical library.

It's bloated, but it's the best alternative 99% of the time. Using macros here is not worth the many downsides.

(EDIT: I've amended the code to reflect Boost/STL conventions as pointed out by Daniel in his comment.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...