last update 20 Sep 2009 |
#include <cvrMetaProgrammingTools.h>
It is recommended to use enum values or static const int members for specifying X
as well as the case parameters.
The following examples clarify the syntax. They all use the following setup, that defines a type foo<X>::type that is dependent on the value of X:
enum VAL{One, Two, Three}; template <VAL X> struct foo { typedef typename internal::switch_t<X, .....>::type type; }
The most simple case is just a default type:
typedef typename internal::switch_t<X, internal::default_t<int> >::type type;
Just one case could have been done using if_t as well, but like this it is more easily extended. Here, if X
== One
then type
is float
otherwise type
is int
.
typedef typename internal::switch_t<X, internal::case_t<One, float, internal::default_t<int> > >::type type;
Finally, and recommended, include all possible values as cases in the enum and supply a default that will lead to a notification of some sort. Note that it is considered bad style not to provide a default.
typedef typename internal::switch_t<X, internal::case_t<One, int, internal::case_t<Two, float, internal::case_t<Three, std::complex<float>, internal::default_t<void> > > > >::type type;