summaryrefslogtreecommitdiff
path: root/libs/type_traits/test/common_type_2_test.cpp
blob: 8a4e74655db1a6ed6c2cc17c8a3d0f8a86b0144f (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//  common_type_test.cpp  ----------------------------------------------------//

//  Copyright 2010 Beman Dawes

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

#define BOOST_COMMON_TYPE_DONT_USE_TYPEOF 1

#include "test.hpp"
#include "check_type.hpp"
#ifdef TEST_STD
#  include <type_traits>
#else
#  include <boost/type_traits/common_type.hpp>
#endif
#include <iostream>

#ifdef BOOST_INTEL
#pragma warning(disable: 304 383)
#endif

struct C1 {};
    
struct C2 {};

    
struct C3 : C2 {};
struct C1C2 {
    C1C2() {}
    C1C2(C1 const&) {}
    C1C2(C2 const&) {}
    C1C2& operator=(C1C2 const&) {
        return *this;
    }
};

template <typename C, typename A>
void proc2(typename boost::common_type<A, C>::type const& ) {}

template <typename C, typename A, typename B>
void proc3(typename boost::common_type<C, A, B>::type const& ) {}

template <typename C, typename A>
void assignation_2() {
typedef typename boost::common_type<A, C>::type AC;
    A a;
    C c;
    AC ac;
    ac=a;
    ac=c;

    proc2<C, A>(a);
    proc2<C, A>(c);
    
}

template <typename C, typename A, typename B>
void assignation_3() {
typedef typename boost::common_type<C, A, B>::type ABC;
    A a;
    B b;
    C c;
    ABC abc;
    
    abc=a;
    abc=b;
    abc=c;
    
    proc3<C, A, B>(a);
    proc3<C, A, B>(b);
    proc3<C, A, B>(c);
}

C1C2 c1c2;
C1 c1;

int f(C1C2 ) { return 1;}
int f(C1 ) { return 2;}
template <typename OSTREAM>
OSTREAM& operator<<(OSTREAM& os, C1 const&) {return os;}

C1C2& declval_C1C2() {return c1c2;}
C1& declval_C1(){return c1;}
bool declval_bool(){return true;}


TT_TEST_BEGIN(common_type)
{
#ifndef __SUNPRO_CC
    assignation_2<C1C2, C1>();
    typedef tt::common_type<C1C2&, C1&>::type T1;
	BOOST_CHECK_TYPE(T1, C1C2);
    typedef tt::common_type<C3*, C2*>::type T2;
	BOOST_CHECK_TYPE(T2, C2*);
    typedef tt::common_type<int*, int const*>::type T3;
	BOOST_CHECK_TYPE(T3, int const*);
#if defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_COMMON_TYPE_DONT_USE_TYPEOF)
    // fails if BOOST_COMMON_TYPE_DONT_USE_TYPEOF:
    typedef tt::common_type<int volatile*, int const*>::type T4;
	BOOST_CHECK_TYPE(T4, int const volatile*);
#endif
    typedef tt::common_type<int*, int volatile*>::type T5;
	BOOST_CHECK_TYPE(T5, int volatile*);

    assignation_2<C1, C1C2>();
    assignation_2<C1C2, C2>();
    assignation_2<C2, C1C2>();
    assignation_3<C1, C1C2, C2>();
    assignation_3<C1C2, C1, C2>();
    assignation_3<C2, C1C2, C1>();
    assignation_3<C1C2, C2, C1>();
    //assignation_3<C1, C2, C1C2>(); // fails because the common type is the third
#endif

    typedef tt::common_type<C1C2, C1>::type t1;
    BOOST_CHECK_TYPE(t1, C1C2);

    BOOST_CHECK_TYPE(tt::common_type<int>::type, int);
    BOOST_CHECK_TYPE(tt::common_type<char>::type, char);
    
    BOOST_CHECK_TYPE3(tt::common_type<char, char>::type, char);
    BOOST_CHECK_TYPE3(tt::common_type<char, unsigned char>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<char, short>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<char, unsigned short>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<char, int>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<char, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
    BOOST_CHECK_TYPE3(tt::common_type<char, boost::long_long_type>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<char, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
    BOOST_CHECK_TYPE3(tt::common_type<char, double>::type, double);

    BOOST_CHECK_TYPE3(tt::common_type<unsigned char, char>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned char>::type, unsigned char);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned char, short>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned short>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned char, int>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
    BOOST_CHECK_TYPE3(tt::common_type<unsigned char, boost::long_long_type>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned char, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
    BOOST_CHECK_TYPE3(tt::common_type<unsigned char, double>::type, double);
    
    BOOST_CHECK_TYPE3(tt::common_type<short, char>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<short, unsigned char>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<short, short>::type, short);
    BOOST_CHECK_TYPE3(tt::common_type<short, unsigned short>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<short, int>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<short, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
    BOOST_CHECK_TYPE3(tt::common_type<short, boost::long_long_type>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<short, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
    BOOST_CHECK_TYPE3(tt::common_type<short, double>::type, double);

    BOOST_CHECK_TYPE3(tt::common_type<unsigned short, char>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned char>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned short, short>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned short>::type, unsigned short);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned short, int>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
    BOOST_CHECK_TYPE3(tt::common_type<unsigned short, boost::long_long_type>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned short, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
    BOOST_CHECK_TYPE3(tt::common_type<unsigned short, double>::type, double);

    BOOST_CHECK_TYPE3(tt::common_type<int, char>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<int, unsigned char>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<int, short>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<int, unsigned short>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<int, int>::type, int);
    BOOST_CHECK_TYPE3(tt::common_type<int, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
    BOOST_CHECK_TYPE3(tt::common_type<int, boost::long_long_type>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<int, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
    BOOST_CHECK_TYPE3(tt::common_type<int, double>::type, double);

    BOOST_CHECK_TYPE3(tt::common_type<unsigned int, char>::type, unsigned int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned char>::type, unsigned int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned int, short>::type, unsigned int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned short>::type, unsigned int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned int, int>::type, unsigned int);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
    BOOST_CHECK_TYPE3(tt::common_type<unsigned int, boost::long_long_type>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<unsigned int, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
    BOOST_CHECK_TYPE3(tt::common_type<unsigned int, double>::type, double);

#ifndef BOOST_NO_LONG_LONG
    BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, char>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned char>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, short>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned short>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, int>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned int>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, boost::long_long_type>::type, boost::long_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, boost::ulong_long_type>::type, boost::ulong_long_type);
    BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, double>::type, double);
#endif
    BOOST_CHECK_TYPE3(tt::common_type<double, char>::type, double);
    BOOST_CHECK_TYPE3(tt::common_type<double, unsigned char>::type, double);
    BOOST_CHECK_TYPE3(tt::common_type<double, short>::type, double);
    BOOST_CHECK_TYPE3(tt::common_type<double, unsigned short>::type, double);
    BOOST_CHECK_TYPE3(tt::common_type<double, int>::type, double);
    BOOST_CHECK_TYPE3(tt::common_type<double, unsigned int>::type, double);
#ifndef BOOST_NO_LONG_LONG
    BOOST_CHECK_TYPE3(tt::common_type<double, boost::long_long_type>::type, double);
    BOOST_CHECK_TYPE3(tt::common_type<double, boost::ulong_long_type>::type, double);
#endif
    BOOST_CHECK_TYPE3(tt::common_type<double, double>::type, double);
    
    BOOST_CHECK_TYPE4(tt::common_type<double, char, int>::type, double);
#ifndef BOOST_NO_LONG_LONG
    BOOST_CHECK_TYPE4(tt::common_type<unsigned, char, boost::long_long_type>::type, boost::long_long_type);
#endif
}
TT_TEST_END