-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_enable_if.cpp
55 lines (40 loc) · 1.12 KB
/
07_enable_if.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
template <bool B, class T = void>
struct enable_if {};
template <class T>
struct enable_if<true, T> { typedef T type; };
template <bool B, class T>
using enable_if_t = typename enable_if<B, T>::type;
template <class T>
struct is_integral {};
template <>
struct is_integral<int>
{
const static bool value = true;
};
template <class T>
struct is_floating_point {};
template <>
struct is_floating_point<float>
{
const static bool value = true;
};
struct T
{
enum { INT, FLOAT } type;
template <typename Integer, enable_if_t<is_integral<Integer>::value, bool> = true>
T(Integer) : type(INT) {}
template <typename Floating, enable_if_t<is_floating_point<Floating>::value, bool> = true>
T(Floating) : type(FLOAT) {}
};
int main()
{
// std::cout << enable_if_t<true, int>(3.14) << "\n";
// std::cout << enable_if_t<true, float>(3.14) << "\n";
// std::cout << enable_if_t<false, int>(3.14) << "\n";
// std::cout << T(1).type << "\n";
// std::cout << T(3.14f).type << "\n";
// std::cout << T(3.14).type << "\n";
// std::cout << T("okey").type << "\n";
return 0;
}