1 // boost::compressed_pair test program
3 // (C) Copyright John Maddock 2000.
4 // Use, modification and distribution are subject to the Boost Software License,
5 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt).
9 // standalone test program for <boost/call_traits.hpp>
11 // Changed some names to prevent conflicts with some new type_traits additions.
13 // Enabled extra tests for VC6.
19 #include <boost/call_traits.hpp>
21 #include <libs/type_traits/test/test.hpp>
22 #include <libs/type_traits/test/check_type.hpp>
25 #pragma warning(disable:4181) // : warning C4181: qualifier applied to reference type; ignored
28 // a way prevent warnings for unused variables
29 template<class T
> inline void unused_variable(const T
&) {}
32 // struct contained models a type that contains a type (for example std::pair)
33 // arrays are contained by value, and have to be treated as a special case:
38 // define our typedefs first, arrays are stored by value
39 // so value_type is not the same as result_type:
40 typedef typename
boost::call_traits
<T
>::param_type param_type
;
41 typedef typename
boost::call_traits
<T
>::reference reference
;
42 typedef typename
boost::call_traits
<T
>::const_reference const_reference
;
44 typedef typename
boost::call_traits
<T
>::value_type result_type
;
51 contained(param_type p
) : v_(p
){}
53 result_type
value()const { return v_
; }
55 reference
get() { return v_
; }
56 const_reference
const_get()const { return v_
; }
58 void call(param_type
){}
60 contained
& operator=(const contained
&);
63 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
64 template <class T
, std::size_t N
>
65 struct contained
<T
[N
]>
67 typedef typename
boost::call_traits
<T
[N
]>::param_type param_type
;
68 typedef typename
boost::call_traits
<T
[N
]>::reference reference
;
69 typedef typename
boost::call_traits
<T
[N
]>::const_reference const_reference
;
70 typedef T value_type
[N
];
71 typedef typename
boost::call_traits
<T
[N
]>::value_type result_type
;
75 contained(param_type p
)
77 std::copy(p
, p
+N
, v_
);
80 result_type
value()const { return v_
; }
82 reference
get() { return v_
; }
83 const_reference
const_get()const { return v_
; }
84 void call(param_type
){}
86 contained
& operator=(const contained
&);
91 contained
<typename
boost::call_traits
<T
>::value_type
> test_wrap_type(const T
& t
)
93 typedef typename
boost::call_traits
<T
>::value_type ct
;
94 return contained
<ct
>(t
);
99 template <class T1
, class T2
>
101 typename
boost::call_traits
<T1
>::value_type
,
102 typename
boost::call_traits
<T2
>::value_type
>
103 make_pair(const T1
& t1
, const T2
& t2
)
106 typename
boost::call_traits
<T1
>::value_type
,
107 typename
boost::call_traits
<T2
>::value_type
>(t1
, t2
);
115 // struct call_traits_checker:
116 // verifies behaviour of contained example:
119 struct call_traits_checker
121 typedef typename
boost::call_traits
<T
>::param_type param_type
;
122 void operator()(param_type
);
126 void call_traits_checker
<T
>::operator()(param_type p
)
130 cout
<< "checking contained<" << typeid(T
).name() << ">..." << endl
;
131 BOOST_CHECK(t
== c
.value());
132 BOOST_CHECK(t
== c
.get());
133 BOOST_CHECK(t
== c
.const_get());
135 //cout << "typeof contained<" << typeid(T).name() << ">::v_ is: " << typeid(&contained<T>::v_).name() << endl;
136 cout
<< "typeof contained<" << typeid(T
).name() << ">::value() is: " << typeid(&contained
<T
>::value
).name() << endl
;
137 cout
<< "typeof contained<" << typeid(T
).name() << ">::get() is: " << typeid(&contained
<T
>::get
).name() << endl
;
138 cout
<< "typeof contained<" << typeid(T
).name() << ">::const_get() is: " << typeid(&contained
<T
>::const_get
).name() << endl
;
139 cout
<< "typeof contained<" << typeid(T
).name() << ">::call() is: " << typeid(&contained
<T
>::call
).name() << endl
;
144 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
145 template <class T
, std::size_t N
>
146 struct call_traits_checker
<T
[N
]>
148 typedef typename
boost::call_traits
<T
[N
]>::param_type param_type
;
149 void operator()(param_type t
)
151 contained
<T
[N
]> c(t
);
152 cout
<< "checking contained<" << typeid(T
[N
]).name() << ">..." << endl
;
154 for(i
= 0; i
< N
; ++i
)
155 BOOST_CHECK(t
[i
] == c
.value()[i
]);
156 for(i
= 0; i
< N
; ++i
)
157 BOOST_CHECK(t
[i
] == c
.get()[i
]);
158 for(i
= 0; i
< N
; ++i
)
159 BOOST_CHECK(t
[i
] == c
.const_get()[i
]);
161 cout
<< "typeof contained<" << typeid(T
[N
]).name() << ">::v_ is: " << typeid(&contained
<T
[N
]>::v_
).name() << endl
;
162 cout
<< "typeof contained<" << typeid(T
[N
]).name() << ">::value is: " << typeid(&contained
<T
[N
]>::value
).name() << endl
;
163 cout
<< "typeof contained<" << typeid(T
[N
]).name() << ">::get is: " << typeid(&contained
<T
[N
]>::get
).name() << endl
;
164 cout
<< "typeof contained<" << typeid(T
[N
]).name() << ">::const_get is: " << typeid(&contained
<T
[N
]>::const_get
).name() << endl
;
165 cout
<< "typeof contained<" << typeid(T
[N
]).name() << ">::call is: " << typeid(&contained
<T
[N
]>::call
).name() << endl
;
173 template <class W
, class U
>
174 void check_wrap(const W
& w
, const U
& u
)
176 cout
<< "checking " << typeid(W
).name() << "..." << endl
;
177 BOOST_CHECK(w
.value() == u
);
182 // verifies behaviour of "make_pair":
184 template <class T
, class U
, class V
>
185 void check_make_pair(T c
, U u
, V v
)
187 cout
<< "checking std::pair<" << typeid(c
.first
).name() << ", " << typeid(c
.second
).name() << ">..." << endl
;
188 BOOST_CHECK(c
.first
== u
);
189 BOOST_CHECK(c
.second
== v
);
194 struct comparible_UDT
197 comparible_UDT() : i_(2){}
198 comparible_UDT(const comparible_UDT
& other
) : i_(other
.i_
){}
199 comparible_UDT
& operator=(const comparible_UDT
& other
)
204 bool operator == (const comparible_UDT
& v
){ return v
.i_
== i_
; }
209 call_traits_checker
<comparible_UDT
> c1
;
212 call_traits_checker
<int> c2
;
213 call_traits_checker
<enum_UDT
> c2b
;
219 #if defined(BOOST_MSVC6_MEMBER_TEMPLATES) && !defined(__ICL)
220 call_traits_checker
<int*> c3
;
222 call_traits_checker
<int&> c4
;
224 call_traits_checker
<const int&> c5
;
226 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__MWERKS__) && !defined(__SUNPRO_CC)
227 call_traits_checker
<int[2]> c6
;
232 check_wrap(test_wrap_type(2), 2);
233 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__SUNPRO_CC)
234 check_wrap(test_wrap_type(a
), a
);
235 check_make_pair(test::make_pair(a
, a
), a
, a
);
238 // cv-qualifiers applied to reference types should have no effect
239 // declare these here for later use with is_reference and remove_reference:
241 typedef const r_type cr_type
;
243 BOOST_CHECK_TYPE(comparible_UDT
, boost::call_traits
<comparible_UDT
>::value_type
);
244 BOOST_CHECK_TYPE(comparible_UDT
&, boost::call_traits
<comparible_UDT
>::reference
);
245 BOOST_CHECK_TYPE(const comparible_UDT
&, boost::call_traits
<comparible_UDT
>::const_reference
);
246 BOOST_CHECK_TYPE(const comparible_UDT
&, boost::call_traits
<comparible_UDT
>::param_type
);
247 BOOST_CHECK_TYPE(int, boost::call_traits
<int>::value_type
);
248 BOOST_CHECK_TYPE(int&, boost::call_traits
<int>::reference
);
249 BOOST_CHECK_TYPE(const int&, boost::call_traits
<int>::const_reference
);
250 BOOST_CHECK_TYPE(const int, boost::call_traits
<int>::param_type
);
251 BOOST_CHECK_TYPE(int*, boost::call_traits
<int*>::value_type
);
252 BOOST_CHECK_TYPE(int*&, boost::call_traits
<int*>::reference
);
253 BOOST_CHECK_TYPE(int*const&, boost::call_traits
<int*>::const_reference
);
254 BOOST_CHECK_TYPE(int*const, boost::call_traits
<int*>::param_type
);
255 #if defined(BOOST_MSVC6_MEMBER_TEMPLATES)
256 BOOST_CHECK_TYPE(int&, boost::call_traits
<int&>::value_type
);
257 BOOST_CHECK_TYPE(int&, boost::call_traits
<int&>::reference
);
258 BOOST_CHECK_TYPE(const int&, boost::call_traits
<int&>::const_reference
);
259 BOOST_CHECK_TYPE(int&, boost::call_traits
<int&>::param_type
);
260 #if !(defined(__GNUC__) && ((__GNUC__ < 3) || (__GNUC__ == 3) && (__GNUC_MINOR__ < 1)))
261 BOOST_CHECK_TYPE(int&, boost::call_traits
<cr_type
>::value_type
);
262 BOOST_CHECK_TYPE(int&, boost::call_traits
<cr_type
>::reference
);
263 BOOST_CHECK_TYPE(const int&, boost::call_traits
<cr_type
>::const_reference
);
264 BOOST_CHECK_TYPE(int&, boost::call_traits
<cr_type
>::param_type
);
266 std::cout
<< "Your compiler cannot instantiate call_traits<int&const>, skipping four tests (4 errors)" << std::endl
;
268 BOOST_CHECK_TYPE(const int&, boost::call_traits
<const int&>::value_type
);
269 BOOST_CHECK_TYPE(const int&, boost::call_traits
<const int&>::reference
);
270 BOOST_CHECK_TYPE(const int&, boost::call_traits
<const int&>::const_reference
);
271 BOOST_CHECK_TYPE(const int&, boost::call_traits
<const int&>::param_type
);
272 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
273 BOOST_CHECK_TYPE(const int*, boost::call_traits
<int[3]>::value_type
);
274 BOOST_CHECK_TYPE(int(&)[3], boost::call_traits
<int[3]>::reference
);
275 BOOST_CHECK_TYPE(const int(&)[3], boost::call_traits
<int[3]>::const_reference
);
276 BOOST_CHECK_TYPE(const int*const, boost::call_traits
<int[3]>::param_type
);
277 BOOST_CHECK_TYPE(const int*, boost::call_traits
<const int[3]>::value_type
);
278 BOOST_CHECK_TYPE(const int(&)[3], boost::call_traits
<const int[3]>::reference
);
279 BOOST_CHECK_TYPE(const int(&)[3], boost::call_traits
<const int[3]>::const_reference
);
280 BOOST_CHECK_TYPE(const int*const, boost::call_traits
<const int[3]>::param_type
);
281 // test with abstract base class:
282 BOOST_CHECK_TYPE(test_abc1
, boost::call_traits
<test_abc1
>::value_type
);
283 BOOST_CHECK_TYPE(test_abc1
&, boost::call_traits
<test_abc1
>::reference
);
284 BOOST_CHECK_TYPE(const test_abc1
&, boost::call_traits
<test_abc1
>::const_reference
);
285 BOOST_CHECK_TYPE(const test_abc1
&, boost::call_traits
<test_abc1
>::param_type
);
287 std::cout
<< "You're compiler does not support partial template specialiation, skipping 8 tests (8 errors)" << std::endl
;
290 std::cout
<< "You're compiler does not support partial template specialiation, skipping 20 tests (20 errors)" << std::endl
;
292 // test with an incomplete type:
293 BOOST_CHECK_TYPE(incomplete_type
, boost::call_traits
<incomplete_type
>::value_type
);
294 BOOST_CHECK_TYPE(incomplete_type
&, boost::call_traits
<incomplete_type
>::reference
);
295 BOOST_CHECK_TYPE(const incomplete_type
&, boost::call_traits
<incomplete_type
>::const_reference
);
296 BOOST_CHECK_TYPE(const incomplete_type
&, boost::call_traits
<incomplete_type
>::param_type
);
298 BOOST_CHECK_TYPE(enum_UDT
, boost::call_traits
<enum_UDT
>::value_type
);
299 BOOST_CHECK_TYPE(enum_UDT
&, boost::call_traits
<enum_UDT
>::reference
);
300 BOOST_CHECK_TYPE(const enum_UDT
&, boost::call_traits
<enum_UDT
>::const_reference
);
301 BOOST_CHECK_TYPE(const enum_UDT
, boost::call_traits
<enum_UDT
>::param_type
);
306 // define call_traits tests to check that the assertions in the docs do actually work
307 // this is an compile-time only set of tests:
309 template <typename T
, bool isarray
= false>
310 struct call_traits_test
312 typedef ::boost::call_traits
<T
> ct
;
313 typedef typename
ct::param_type param_type
;
314 typedef typename
ct::reference reference
;
315 typedef typename
ct::const_reference const_reference
;
316 typedef typename
ct::value_type value_type
;
317 static void assert_construct(param_type val
);
320 template <typename T
, bool isarray
>
321 void call_traits_test
<T
, isarray
>::assert_construct(typename call_traits_test
<T
, isarray
>::param_type val
)
324 // this is to check that the call_traits assertions are valid:
328 const_reference
cr(t
);
335 const_reference
cr2(v
);
336 const_reference
cr3(r
);
337 const_reference
cr4(cr
);
338 const_reference
cr5(p
);
348 unused_variable(cr2
);
349 unused_variable(cr3
);
350 unused_variable(cr4
);
351 unused_variable(cr5
);
356 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
357 template <typename T
>
358 struct call_traits_test
<T
, true>
360 typedef ::boost::call_traits
<T
> ct
;
361 typedef typename
ct::param_type param_type
;
362 typedef typename
ct::reference reference
;
363 typedef typename
ct::const_reference const_reference
;
364 typedef typename
ct::value_type value_type
;
365 static void assert_construct(param_type val
);
368 template <typename T
>
369 void call_traits_test
<T
, true>::assert_construct(typename
boost::call_traits
<T
>::param_type val
)
372 // this is to check that the call_traits assertions are valid:
377 const_reference cr
= t
;
380 // C++ Builder buglet:
381 const_reference cr2
= r
;
385 const_reference cr3
= cr
;
398 unused_variable(cr2
);
400 unused_variable(cr3
);
405 #endif //BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
407 // now check call_traits assertions by instantiating call_traits_test:
408 template struct call_traits_test
<int>;
409 template struct call_traits_test
<const int>;
410 template struct call_traits_test
<int*>;
411 #if defined(BOOST_MSVC6_MEMBER_TEMPLATES)
412 template struct call_traits_test
<int&>;
413 template struct call_traits_test
<const int&>;
414 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__SUNPRO_CC)
415 template struct call_traits_test
<int[2], true>;