1 #include <iostream>
2 #include <type_traits>
3 using namespace std;
4
5 //定义一个person类 class person
6 class person{};
7
8 class dog
9 {
10 };
11
12 template <typename T>
13 struct is_person
14 {
15 static const bool value = false;
16 };
17
18 template <>
19 struct is_person<person>
20 {
21 static const bool value = true;
22 };
23
24 template <typename T, typename std::enable_if<is_person<T>::value, T>::type * = nullptr>
25 void func(T t)
26 {
27 cout << "person stuff" << endl;
28 }
29
30 void func(int a)
31 {
32 cout << "int type" << endl;
33 }
34
35
36 template <typename T, typename std::enable_if<!is_person<T>::value, T>::type * = nullptr>
37 void func(T t)
38 {
39 // do something;
40 cout << "not person stuff" << endl;
41 }
42
43 int main()
44 {
45 func(5); // int type
46 func(person()); // person stuff
47 func(dog()); // not person stuff
48 }