-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenable_if.cpp
63 lines (53 loc) · 1.43 KB
/
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
56
57
58
59
60
61
62
63
#include<iostream>
using namespace std;
#if __cplusplus < 201103L
template<bool,typename=void>
struct enable_if{};
template<typename T>
struct enable_if<true,T>{ typedef T type; };
template<typename T,typename _T>
struct is_same{
static const bool value=false;
};
template<typename T>
struct is_same<T,T>{
static const bool value=true;
};
#endif
/* DEDUCE CLASS (by type) */
template<typename T,typename=T>
struct remap{
void func(){ cout<<"struct for generic types"; };
};
template<typename T>
struct remap<T,typename enable_if<
is_same<T,char>::value
||is_same<T,unsigned char>::value
||is_same<T,signed char>::value
||is_same<T,int>::value
,T>::type>{
void func(){ cout<<"struct for specific types"; };
};
/* DEDUCE FUNCTION OVERLOADS (by argument type) */
template<typename T>
struct remap2{
/* DEDUCE BY FUNC ARGUMENT TYPE (for func overloads) */
void func2(typename enable_if<
is_same<T,char>::value
||is_same<T,unsigned char>::value
||is_same<T,signed char>::value
||is_same<T,int>::value
,T>::type){ cout<<"func for specific types"; };
template<typename Y>
void func2(Y j){ cout<<"func for generic types"; };
};
int main(){
// remap<string> obj;
remap<int> obj;
obj.func();
cout<<endl;
remap2<int> obj2;
// obj2.func2("");
obj2.func2(123);
return 0;
}