| metaprogramming/conditional/cond-2.cc - An sample use of conditional. | Lecture 6 - slide 34 : 40 Program 1 |
// Conditional example - adapted from www.cplusplus.com. Simple stuff.
// Illustrates std::conditional and std:is_same.
// Ilustrates using syntax (4ed, pages 167).
#include <iostream>
#include <type_traits>
int main() {
// Establish aliases for selected types:
using A = std::conditional<true, int, float>::type; // select int
using B = std::conditional<false, int, float>::type; // select float
using C = std::conditional<std::is_integral<A>::value, long, int>::type; // select long - because type A is int
using D = std::conditional<std::is_integral<B>::value, long, int>::type; // select int - because B is float
// Use established types:
A a{}; // A variable of type A
B b = [](B x) -> B{return x/2;}(10.0); // Use of type B in a lambda expressions, and its immediate call
// Print info about types:
std::cout << std::boolalpha; // print boolean values as false and true
// (rather than 0 and 1)
std::cout << "typedefs of int:" << std::endl;
std::cout << "A: " << std::is_same<int,A>::value << std::endl; // A: true
std::cout << "B: " << std::is_same<int,B>::value << std::endl; // B: false
std::cout << "C: " << std::is_same<int,C>::value << std::endl; // C: false
std::cout << "D: " << std::is_same<int,D>::value << std::endl; // D: true
return 0;
}