| metaprogramming/typefunctions/int-types-2.cc - Yet another version of type function - generalized to selection among an arbitrary number of types. | Lecture 6 - slide 38 : 40 Program 1 |
// The example generalized to selection based on a bytelength from 0 to 8.
#include <iostream>
#include <string>
#include "select-stuff-general.cc"
// Integer<N>::type is now defined as 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, // 4 selects int
Error, // 5 selects Error (void)
Error, // 6 selects Error (void)
Error, // 7 selects Error (void)
long long int // 8 selects long long int
>;
};
// Embellish the notation Integer<N>::type to Integer_of_size<N> - made by use of a template alias (4ed, 23.6).
template<int M>
using Integer_of_byte_size = typename Integer<M>::type;
int main(){
using namespace std;
Integer_of_byte_size<1> i = 65; // similar to: unsigned char i = 10; // 'A'
Integer_of_byte_size<2> j = 66; // similar to: short int j = 66;
Integer_of_byte_size<4> k = 67; // similar to: int k = 67;
Integer_of_byte_size<8> l = 68; // similar to: long int l = 68;
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
cout << "k: " << k << " of size: " << sizeof(k) << endl; // k: 67 of size 4
cout << "l: " << l << " of size: " << sizeof(l) << endl; // l: 68 of size 8
}