Debian Codegraph @

yo/advancedtemplates/examples/traits.cc

raw
1 #include <stdint.h>
2
3 #include <iostream>
4 #include <string>
5 #include <vector>
6 #include <list>
7 #include <queue>
8 #include <sstream>
9 #include <algorithm>
10 #include <iterator>
11
12 using namespace std;
13
14 template <int x>
15 struct IntType
16 {
17 enum
18 {
19 value = x
20 };
21 };
22
23
24 template <typename T>
25 class TypeTrait
26 {
27 template <typename U> struct Type
28 {
29 enum { isPointer = false };
30 enum { isConst = false };
31
32 using Plain = U;
33 };
34
35 template <typename U> struct Type<U const>
36 {
37 enum { isPointer = false };
38 enum { isConst = true };
39
40 using Plain = U;
41 };
42
43 using small = char;
44 struct Big {char big[2];};
45
46 template <typename T2>
47 static small fun(...);
48
49 template <typename T2>
50 static Big fun(void (T2::*)());
51
52 // template <typename T2>
53 // static IntType<false> fun(...);
54 //
55 // template <typename T2>
56 // static IntType<true> fun(void (T2::*)());
57
58 public:
59 enum { isConst = Type<T>::isConst };
60 enum { isClass = sizeof(fun<T>(0)) == sizeof(Big) };
61 // enum { isClass = fun<T>(0)::value;
62 };
63
64 int main()
65 {
66 string s;
67 cout << TypeTrait<string>::isClass << '\n';
68 cout << TypeTrait<int>::isClass << '\n';
69 }