| metaprogramming/conditional/cond-3-realistic.cc - Use of my_conditional in the example from above. | Lecture 6 - slide 34 : 40 Program 3 |
// A slightly more realistic use of type selection.
#include <iostream>
#include <type_traits>
// Conditional is a syntactial embellishment of the application of underlying conditional template.
// We are using a template alias:
template<bool B, typename T, typename F>
using Conditional = typename std::conditional<B, T, F>::type;
constexpr long int LIMIT = 5000;
constexpr bool large(int n){
return n > LIMIT;
}
int main() {
using namespace std;
const long int SZ = 1234;
using FloatingPointT = Conditional<large(SZ),double,float>; // select type - float is selected in this
// particular context.
FloatingPointT x = 1234.5678;
cout << "sizeof(double) = " << sizeof(double) << endl; // 8
cout << "sizeof(float) = " << sizeof(float) << endl; // 4
cout << "sizeof(FloatingPointT) = " << sizeof(FloatingPointT) << endl; // 4
return 0;
}