| 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 | |
| 19 | using boost::lazy_enable_if; |
| 20 | using boost::lazy_disable_if; |
| 21 | |
| 22 | using boost::lazy_enable_if_c; |
| 23 | using boost::lazy_disable_if_c; |
| 24 | |
| 25 | |
| 26 | template <class T> |
| 27 | struct 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 | |
| 33 | template <class T> |
| 34 | struct some_traits { |
| 35 | typedef typename T::does_not_exist type; |
| 36 | }; |
| 37 | |
| 38 | template <> |
| 39 | struct some_traits<int> { |
| 40 | typedef bool type; |
| 41 | }; |
| 42 | |
| 43 | template <> |
| 44 | struct some_traits<double> { |
| 45 | typedef bool type; |
| 46 | }; |
| 47 | |
| 48 | template <class T> |
| 49 | struct make_bool { |
| 50 | typedef bool type; |
| 51 | }; |
| 52 | |
| 53 | template <> |
| 54 | struct make_bool<int> {}; |
| 55 | |
| 56 | template <> |
| 57 | struct make_bool<double> {}; |
| 58 | |
| 59 | namespace 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 | |
| 70 | namespace 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 | |
| 80 | int 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 | |