File: traits.cc

package info (click to toggle)
c%2B%2B-annotations 11.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,244 kB
  • sloc: cpp: 21,698; makefile: 1,505; ansic: 165; sh: 121; perl: 90
file content (69 lines) | stat: -rw-r--r-- 1,247 bytes parent folder | download | duplicates (6)
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
64
65
66
67
68
69
#include <stdint.h>

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

template <int x>
struct IntType
{
    enum
    {
        value = x
    };
};


template <typename T>
class TypeTrait
{
    template <typename U> struct Type
    {
        enum { isPointer = false };
        enum { isConst   = false };

        typedef U Plain;
    };

    template <typename U> struct Type<U const>
    {
        enum { isPointer = false };
        enum { isConst   = true };

        typedef U Plain;
    };

    typedef char small;
    struct Big {char big[2];};

    template <typename T2>
    static small fun(...);

    template <typename T2>
    static Big   fun(void (T2::*)());

//    template <typename T2>
//    static IntType<false> fun(...);
//
//    template <typename T2>
//    static IntType<true>  fun(void (T2::*)());

    public:
        enum { isConst     = Type<T>::isConst };
        enum { isClass     = sizeof(fun<T>(0)) == sizeof(Big) };
//        enum { isClass     = fun<T>(0)::value;
};

int main()
{
    string s;
    cout << TypeTrait<string>::isClass << '\n';
    cout << TypeTrait<int>::isClass << '\n';
}