First commit
[cpp_sandbox.git] / Sandbox / Traits.cpp
1 #include "Traits.h"
2
3 #include <iostream>
4 #include <type_traits>
5
6 using namespace std;
7
8 namespace Traits
9 {
10 class C {};
11
12 /////
13
14 void algorithm_signed(int i) { /* [..] */ cout << "process a signed integer" << endl; }
15 void algorithm_unsigned(unsigned) { /* [..] */ cout << "process an unsigned integer" << endl; }
16
17 template <typename T>
18 void algorithm(T t)
19 {
20 if constexpr (is_signed<T>::value)
21 algorithm_signed(t);
22 else if constexpr (is_unsigned<T>::value)
23 algorithm_unsigned(t);
24 else
25 static_assert(is_signed<T>::value || is_unsigned<T>::value, "Must be signed or unsigned");
26 }
27
28 /////
29
30 template <typename T>
31 typename remove_reference<T>::type&& move(T&& arg)
32 {
33 return static_cast<typename remove_reference<T>::type&&>(arg);
34 }
35 }
36
37 void Traits::tests()
38 {
39 cout << "Is the type 'C' a floating point?: " << is_floating_point<C>::value << endl;
40 cout << "Is the type 'float' a floating point?: " << is_floating_point<float>::value << endl;
41 cout << "Is the type 'int' a floating point?:" << is_floating_point<int>::value << endl;
42
43 algorithm(3);
44 algorithm(3U);
45 // algorithm("hello"); Do not compile.
46 }