| metaprogramming/typefunctions/int-types-1.cc - Second version of type function - embellished syntax of the type function. | Lecture 6 - slide 37 : 40 Program 2 |
// Simplifying the use of Select via use of a template alias (4ed page 694).
#include <iostream>
#include <string>
#include "select-stuff.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)
>;
};
// 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 = 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
}