1// Boost enable_if library
2
3// Copyright 2003 (c) The Trustees of Indiana University.
4
5// Use, modification, and distribution is subject to the Boost Software
6// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8
9// Authors: Jaakko Jarvi (jajarvi at osl.iu.edu)
10// Jeremiah Willcock (jewillco at osl.iu.edu)
11// Andrew Lumsdaine (lums at osl.iu.edu)
12
13// Testing all variations of lazy_enable_if.
14
15#include <boost/core/enable_if.hpp>
16#include <boost/type_traits/is_same.hpp>
17#include <boost/core/lightweight_test.hpp>
18
19using boost::lazy_enable_if;
20using boost::lazy_disable_if;
21
22using boost::lazy_enable_if_c;
23using boost::lazy_disable_if_c;
24
25
26template <class T>
27struct is_int_or_double {
28 BOOST_STATIC_CONSTANT(bool,
29 value = (boost::is_same<T, int>::value ||
30 boost::is_same<T, double>::value));
31};
32
33template <class T>
34struct some_traits {
35 typedef typename T::does_not_exist type;
36};
37
38template <>
39struct some_traits<int> {
40 typedef bool type;
41};
42
43template <>
44struct some_traits<double> {
45 typedef bool type;
46};
47
48template <class T>
49struct make_bool {
50 typedef bool type;
51};
52
53template <>
54struct make_bool<int> {};
55
56template <>
57struct make_bool<double> {};
58
59namespace A {
60
61 template<class T>
62 typename lazy_enable_if<is_int_or_double<T>, some_traits<T> >::type
63 foo(T /*t*/) { return true; }
64
65 template<class T>
66 typename lazy_enable_if_c<is_int_or_double<T>::value, some_traits<T> >::type
67 foo2(T /*t*/) { return true; }
68}
69
70namespace B {
71 template<class T>
72 typename lazy_disable_if<is_int_or_double<T>, make_bool<T> >::type
73 foo(T /*t*/) { return false; }
74
75 template<class T>
76 typename lazy_disable_if_c<is_int_or_double<T>::value, make_bool<T> >::type
77 foo2(T /*t*/) { return false; }
78}
79
80int main()
81{
82 using namespace A;
83 using namespace B;
84 BOOST_TEST(foo(1));
85 BOOST_TEST(foo(1.0));
86
87 BOOST_TEST(!foo("1"));
88 BOOST_TEST(!foo(static_cast<void*>(0)));
89
90 BOOST_TEST(foo2(1));
91 BOOST_TEST(foo2(1.0));
92
93 BOOST_TEST(!foo2("1"));
94 BOOST_TEST(!foo2(static_cast<void*>(0)));
95
96 return boost::report_errors();
97}
98

source code of boost/libs/core/test/eif_lazy_test.cpp