| 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 | #include <boost/utility/enable_if.hpp> |
| 14 | #include <boost/type_traits.hpp> |
| 15 | #include <boost/core/lightweight_test.hpp> |
| 16 | #include <cstddef> |
| 17 | |
| 18 | using boost::enable_if; |
| 19 | using boost::disable_if; |
| 20 | using boost::is_arithmetic; |
| 21 | |
| 22 | struct container { |
| 23 | bool my_value; |
| 24 | |
| 25 | template <class T> |
| 26 | container(const T&, const typename enable_if<is_arithmetic<T>, T>::type * = 0): |
| 27 | my_value(true) {} |
| 28 | |
| 29 | template <class T> |
| 30 | container(const T&, const typename disable_if<is_arithmetic<T>, T>::type * = 0): |
| 31 | my_value(false) {} |
| 32 | }; |
| 33 | |
| 34 | // example from Howard Hinnant (tests enable_if template members of a templated class) |
| 35 | template <class charT> |
| 36 | struct xstring |
| 37 | { |
| 38 | template <class It> |
| 39 | xstring(It begin, It end, typename |
| 40 | disable_if<is_arithmetic<It> >::type* = 0) |
| 41 | : data(end-begin) {} |
| 42 | |
| 43 | std::ptrdiff_t data; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | int main() |
| 48 | { |
| 49 | |
| 50 | BOOST_TEST(container(1).my_value); |
| 51 | BOOST_TEST(container(1.0).my_value); |
| 52 | |
| 53 | BOOST_TEST(!container("1" ).my_value); |
| 54 | BOOST_TEST(!container(static_cast<void*>(0)).my_value); |
| 55 | |
| 56 | char sa[] = "123456" ; |
| 57 | BOOST_TEST(xstring<char>(sa, sa+6).data == 6); |
| 58 | |
| 59 | |
| 60 | return boost::report_errors(); |
| 61 | } |
| 62 | |