38180

Is there a standard construct for 'choose'?

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.

Recommend

  • MVC3 - Conditionally add id element with Razor
  • Can I put condition in emacs lisp macro?
  • Application Express: Anonymous PL/SQL Block and Bind Variables
  • When querying against a view, a filtering clause in the view's definition is being ignored
  • Multiple sockets for clients to connect to
  • Complex trait requirements on struct
  • iOS - Is this a task for enums?
  • Generate a unique string based on a pair of strings
  • Deleting a widget from QTableView
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Monotouch crashes with NullReferenceException on non nullable object
  • sweetalert2 inputoptions from file in select example
  • C++ Single function pointer for all template instances
  • SyntaxError: (irb):26: both block arg and actual block given
  • Simple linked list-C
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • Build Successful but not running on simulator
  • How to use carriage return with multiple line?
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • Scrapy recursive link crawler
  • How to rebase a series of branches?
  • Control modification in presentation layer
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Adding custom controls to a full screen movie
  • Why winpcap requires both .lib and .dll to run?
  • Confusion with PayPal's monthly billing cycle
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal