metaprogramming/typefunctions/int-types.cc - First version of type function. | Lecture 6 - slide 37 : 40 Program 1 |
// Use of the Select template struct for selection anmong a number of different types. // Select corresponds to the switch control structure. #include <iostream> #include <string> #include "select-stuff.cc" // Integer<N>::type is an integer type of N bytes: (4ed, page 782) template<int N> struct Integer{ using Error = void; using type = Select<N, Error, // 0 selects Error (void) signed char, // 1 selects singed char short int, // 2 selects short int Error // 3 selects Error (void) >; }; int main(){ using namespace std; typename Integer<1>::type i = 65; // similar to: unsigned char i = 10; // 'A' typename Integer<2>::type j = 66; // similar to: short int j = 8; cout << "i: " << i << " of size: " << sizeof(i) << endl; // i: A of size 1 cout << "j: " << j << " of size: " << sizeof(j) << endl; // j: 66 of size 2 }