Xeo
Xeo

Reputation: 131837

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 type, not a bool) and I don't want to include Boost for this little thing. :)

Upvotes: 3

Views: 143

Answers (1)

James McNellis
James McNellis

Reputation: 355217

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.

Upvotes: 6

Related Questions