#include <senf/Utils/mpl.hh>
The senf::mpl::rv type is used together with SENF_MPL_RV() to select template specializations based on a set of overloads:
template <unsigned _> struct select {}; // Case 0 template <> struct select<0> { static bool const has_int_value = true; void frobble(); }; template <class T> senf::mpl::rv<0> select_(int, senf::mpl::take_int<T::value> * = 0); // Case 1 template <> struct select<1> { static bool const has_int_value = false; void dazzle(); }; template <class T> senf::mpl::rv<1> select_(...); template <class T> struct choice : public select<SENF_MPL_RV( select_<T>(0) )> {}; struct A { static const int value = 0; }; struct B {}; choice<A> a; a.frobble(); choice<B> b; b.dazzle();
The selection is always based on two components: A selector class specialized for each of the possible choices and an overloaded function (only signatures, no implementation needed) to provide the conditions.
When instantiatinv choice<T>
, we forward T to the select_
set of overloads. Because of SFINAE, the overload set will only contain those instantiations, for which template expansion does not fail.
So, if T has an integer value
member, both select_
overloads are ok and the call select_<T>(0)
will choose the first (case 0) variant, since the argument 0
is better matched by int
than by ...
.
However, if T does not have an integer value
member, expansion for the first overload fails and the overload set only contains the second case.
SENF_MPL_RV() internally uses sizeof
to find out, which overload was selected and returns the senf::mpl::rv-argument of that overloads return type. For this to work, the select_
functions need not be implemented since no code is generated and select_
is never called.
This number is than forwarded as template argument to select
which is specialized for each case. Therefore, choice<A>
has a frobble()
member whereas choice<B>
has a dazzle()
member.
Definition at line 116 of file mpl.hh.
Public Attributes |
|
char | _ [SENF_MPL_RV_ALIGNMENT][n+1] |
char senf::mpl::rv< n >::_ | ||||
_[SENF_MPL_RV_ALIGNMENT][n+1] | ||||