38180

Question:
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? <a href="http://www.boost.org/doc/libs/1_46_1/libs/mpl/doc/refmanual/if.html" rel="nofollow">Boost.MPL has something similar</a>, but that isn't exactly the same (takes a type, not a bool) and I don't want to include Boost for this little thing. :)
Answer1: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.