aboutsummaryrefslogtreecommitdiff
path: root/test/libcxx/type_traits/lazy_metafunctions.pass.cpp
blob: 8f75080ab956026ae3e3df5606f2d1bbad0db80c (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++98, c++03

// <type_traits>

// __lazy_enable_if, __lazy_not, __lazy_and and __lazy_or

// Test the libc++ lazy meta-programming helpers in <type_traits>

#include <type_traits>

template <class Type>
struct Identity {
    typedef Type type;
};

typedef std::true_type TrueT;
typedef std::false_type FalseT;

typedef Identity<TrueT>  LazyTrueT;
typedef Identity<FalseT> LazyFalseT;

// A type that cannot be instantiated
template <class T>
struct CannotInst {
    typedef T type;
    static_assert(std::is_same<T, T>::value == false, "");
};


template <int Value>
struct NextInt {
    typedef NextInt<Value + 1> type;
    static const int value = Value;
};

template <int Value>
const int NextInt<Value>::value;


template <class Type>
struct HasTypeImp {
    template <class Up, class = typename Up::type>
    static TrueT test(int);
    template <class>
    static FalseT test(...);

    typedef decltype(test<Type>(0)) type;
};

// A metafunction that returns True if Type has a nested 'type' typedef
// and false otherwise.
template <class Type>
struct HasType : HasTypeImp<Type>::type {};

void LazyEnableIfTest() {
    {
        typedef std::__lazy_enable_if<true, NextInt<0> > Result;
        static_assert(HasType<Result>::value, "");
        static_assert(Result::type::value == 1, "");
    }
    {
        typedef std::__lazy_enable_if<false, CannotInst<int> > Result;
        static_assert(!HasType<Result>::value, "");
    }
}

void LazyNotTest() {
    {
        typedef std::__lazy_not<LazyTrueT> NotT;
        static_assert(std::is_same<typename NotT::type, FalseT>::value, "");
        static_assert(NotT::value == false, "");
    }
    {
        typedef std::__lazy_not<LazyFalseT> NotT;
        static_assert(std::is_same<typename NotT::type, TrueT>::value, "");
        static_assert(NotT::value == true, "");
    }
    {
         // Check that CannotInst<int> is not instantiated.
        typedef std::__lazy_not<CannotInst<int> > NotT;

        static_assert(std::is_same<NotT, NotT>::value, "");

    }
}

void LazyAndTest() {
    { // Test that it acts as the identity function for a single value
        static_assert(std::__lazy_and<LazyFalseT>::value == false, "");
        static_assert(std::__lazy_and<LazyTrueT>::value == true, "");
    }
    {
        static_assert(std::__lazy_and<LazyTrueT, LazyTrueT>::value == true, "");
        static_assert(std::__lazy_and<LazyTrueT, LazyFalseT>::value == false, "");
        static_assert(std::__lazy_and<LazyFalseT, LazyTrueT>::value == false, "");
        static_assert(std::__lazy_and<LazyFalseT, LazyFalseT>::value == false, "");
    }
    { // Test short circuiting - CannotInst<T> should never be instantiated.
        static_assert(std::__lazy_and<LazyFalseT, CannotInst<int>>::value == false, "");
        static_assert(std::__lazy_and<LazyTrueT, LazyFalseT, CannotInst<int>>::value == false, "");
    }
}


void LazyOrTest() {
    { // Test that it acts as the identity function for a single value
        static_assert(std::__lazy_or<LazyFalseT>::value == false, "");
        static_assert(std::__lazy_or<LazyTrueT>::value == true, "");
    }
    {
        static_assert(std::__lazy_or<LazyTrueT, LazyTrueT>::value == true, "");
        static_assert(std::__lazy_or<LazyTrueT, LazyFalseT>::value == true, "");
        static_assert(std::__lazy_or<LazyFalseT, LazyTrueT>::value == true, "");
        static_assert(std::__lazy_or<LazyFalseT, LazyFalseT>::value == false, "");
    }
    { // Test short circuiting - CannotInst<T> should never be instantiated.
        static_assert(std::__lazy_or<LazyTrueT, CannotInst<int>>::value == true, "");
        static_assert(std::__lazy_or<LazyFalseT, LazyTrueT, CannotInst<int>>::value == true, "");
    }
}


int main() {
    LazyEnableIfTest();
    LazyNotTest();
    LazyAndTest();
    LazyOrTest();
}