Group Switch Metafunction Types
Tags for the metafunction Switch and the metafunction itself.

Grouped Tags Overview

Grouped Metafunction Overview

Detailed Description

Example

The following shows a complete example of using the Switch statement.

int switchTest(Nothing const &)
{
    return -1;
}

int switchTest(False const &)
{
    return 0;
}

int switchTest(True const &)
{
    return 1;
}

int switchTest(NilCase const &)
{
    return 2;
}

template <int X>
struct SwitchTest
{
    typedef typename Switch<
        X,
        Case<-1, Nothing,
             Case<0, False,
                  Case<1, True
                       > > > >::Type Type;
};

int main()
{
    typedef SwitchTest<-1>::Type T1;
    typedef SwitchTest<0>::Type T2;
    typedef SwitchTest<1>::Type T3;
    typedef SwitchTest<2>::Type T4;

    std::cout << switchTest(T1()) << "\n"   // => "-1"
              << switchTest(T2()) << "\n"   // => "0"
              << switchTest(T3()) << "\n"   // => "1"
              << switchTest(T4()) << "\n";  // => "2"

    return 0;
}

Grouped Tags Detail

struct NilCase {};

Tag for terminating the case in Switch statement.

Grouped Metafunctions Detail

template <int TAG, typename TResult, typename TNext> struct Case;

Tag for one case.

Template Parameters

TAG The int tag number to use.
TResult The type to return in Case<...>::Type if matches.
TNext The next Case.

Switch<TAG, TCase>::Type

Switch statement for metaprogramming.

Template Parameters

TAG int with the current value.
TCase First Case statement.

Returns

Type The selected type.