Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'                metaprogramming/typefunctions/select-stuff.cc - The ugly and bulky selection stuff.Lecture 6 - slide 37 : 40
Program 3

// Select stuff - templates that define selection of one out of four types (4ed, page 792).

struct Nil {};   // A trivial Nil type

// select - declared, but not defined in the general case. Only specializations are defined.
template <int I, typename T1 = Nil, typename T2 = Nil, typename T3 = Nil, typename T4 = Nil>
struct select;   

// A using alias Select that binds unused types to Nil
template <int I, typename T1 = Nil, typename T2= Nil, typename T3 = Nil, typename T4 = Nil>
using Select = typename select<I, T1, T2, T3, T4>::type;

// Specializations for 0-3
template <typename T1, typename T2, typename T3, typename T4>
struct select<0, T1, T2, T3, T4> {using type = T1;};   // Specialization for 0

template <typename T1, typename T2, typename T3, typename T4>
struct select<1, T1, T2, T3, T4> {using type = T2;};   // Specialization for 1

template <typename T1, typename T2, typename T3, typename T4>
struct select<2, T1, T2, T3, T4> {using type = T3;};   // Specialization for 2

template <typename T1, typename T2, typename T3, typename T4>
struct select<3, T1, T2, T3, T4> {using type = T4;};   // Specialization for 3