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

I sometimes need 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 select one or the other type. Now, is there anything in the standard library that does just that? Boost.MPL has something similar , but it's not quite the same (takes a type, not a bool), and I don't want to enable Boost for this little thing. :)

+3
source share
1 answer

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

boost::mpl::ifThe one you quote has a corresponding boost::mpl::if_cone that instead accepts bool; This is a common pattern in Boost type type libraries.

+6
source

All Articles