开发者

Is there a standard construct for 'choose<bool,typename,typename>'?

I sometimes find myself in need for the following:

template<bool B, typename T1, typename T2>
struct choose{
  typedef T1 type;
};

template<typename T1, typename T2>
struct choose<false, T1, T2>{
  typedef T2 type;
};

I use this to conditionally choose one type or the other. Now, is there already something in the standard library that does exactly this? Boost.MPL has something similar, but that isn't exactly the same (takes a t开发者_JS百科ype, not a bool) and I don't want to include Boost for this little thing. :)


Yes: it is called std::conditional in C++0x (or boost::conditional in Boost).

The boost::mpl::if that you cite has a corresponding boost::mpl::if_c that takes a bool instead of a type; this is a common pattern in the Boost type traits libraries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜