senf::mpl::rv< n > Struct Template Reference

Return-value type used to implement overload selection. More...

#include <senf/Utils/mpl.hh>

Public Attributes

char _ [SENF_MPL_RV_ALIGNMENT][n+1]
 

Detailed Description

template<unsigned n>
struct senf::mpl::rv< n >

Return-value type used to implement overload selection.

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>
// 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 instantiating <tt>choice<T></tt>, we forward \a T to the <tt>select_</tt> set of
    overloads. Because of <a href="http://en.wikipedia.org/wiki/SFINAE">SFINAE</a>, the overload
    set will only contain those instantiations, for which template expansion does not fail.

    So, if \a T has an integer \c value member, both \c select_ overloads are ok and the call
    <tt>select_<T>(0)</tt> will choose the first (case 0) variant, since the argument \c 0 is
    better matched by \c int than by <tt>...</tt>.

    However, if \a T does not have an integer \c value member, expansion for the first overload
    fails and the overload set only contains the second case.

    \ref SENF_MPL_RV() internally uses \c sizeof to find out, \e which overload was selected
    and returns the senf::mpl::rv-argument of that overloads return type. For this to work, the
    \c select_ functions need not be implemented since no code is generated and \c select_ is
    never called.

    This number is than forwarded as template argument to \c select which is specialized for
    each case. Therefore, <tt>choice\<A\></tt> has a \c frobble() member whereas
    <tt>choice\<B\></tt> has a \c dazzle() member.

    \see \ref SENF_MPL_RV

Definition at line 107 of file mpl.hh.

Member Data Documentation

◆ _

template<unsigned n>
char senf::mpl::rv< n >::_[SENF_MPL_RV_ALIGNMENT][n+1]

Definition at line 108 of file mpl.hh.


The documentation for this struct was generated from the following file: