First commit
[cpp_sandbox.git] / Sandbox / ConstexprIf.cpp
1 #include "ConstexprIf.h"
2
3 #include <iostream>
4 #include <type_traits>
5 #include <limits>
6 using namespace std;
7
8 // Ep 18: https://www.youtube.com/watch?v=qHgM5UdzPQU
9
10 namespace ConstexprIf
11 {
12 template<typename T>
13 constexpr bool is_integral_has_a_big_max_limit()
14 {
15 if constexpr (is_integral<T>::value && !is_same<bool, T>::value) // Because 'bool' is an integral type.
16 {
17 // Here we can use a '&&' to put this condition in the first 'if' because template do not use short-cirtcuit boolean operators.
18 if constexpr (numeric_limits<T>::max() > numeric_limits<int>::max())
19 return true;
20 }
21
22 return false;
23 }
24
25 template<typename T>
26 auto print_type_info(const T& v)
27 {
28 if constexpr (is_integral_has_a_big_max_limit<T>())
29 return v + 1;
30 else if constexpr (is_floating_point<T>::value)
31 return v + 0.1;
32 else
33 return v;
34 }
35 }
36
37 void ConstexprIf::tests()
38 {
39 cout << print_type_info(5) << endl;
40 cout << print_type_info(5LL) << endl;
41 cout << print_type_info(2.3) << endl;
42 cout << print_type_info("hello") << endl;
43 cout << print_type_info(true) << endl;
44 }