-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenable_if.cpp
More file actions
35 lines (26 loc) · 814 Bytes
/
enable_if.cpp
File metadata and controls
35 lines (26 loc) · 814 Bytes
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
#include <iostream>
#include <string>
#include <type_traits>
using std::cout;
using std::endl;
template <bool Actually_disable> struct Disable {};
template <> struct Disable<true> { using type = int; };
struct T {
enum { int_t, float_t, string } type;
template <typename Integer,
std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
T(Integer) : type(int_t) {}
template <
typename Floating,
std::enable_if_t<std::is_floating_point<Floating>::value, bool> = true>
T(Floating) : type(float_t) {} // OK
template <Disable<false>::type = 0> T(const char *) : type(string) {}
};
int main() {
cout << "Hello world!" << endl;
T t1(42);
T t2(4.2);
// T t3<3>("hello");
cout << "t1.type = " << t1.type << endl;
cout << "t2.type = " << t2.type << endl;
}