metaprogramming/typefunctions/select-stuff-general.cc - General selection stuff - shorter and more elegant - using variadic templates (C++11). | Lecture 6 - slide 38 : 40 Program 2 |
// Select stuff - templates that define selection in the general case (4ed, page 792). // Uses variadic templates (4ed, 809). C++11. // select - declared, but never instantiated. The specialization below will be instantiated template<unsigned int N, typename... Types> struct select; // Specialization that just recurses in the base type: template <unsigned int N, typename T, typename... Types> struct select<N, T, Types...>: select<N-1, Types...>{ }; // Specialization that represent the base case of the recursion. // Defines what type stands for: template<typename T, typename... Types> struct select<0, T, Types...>{ using type = T; }; // A using alias Select that embellish the use of select - as before: template <unsigned int N, typename... Types> using Select = typename select<N, Types...>::type;