eif_constructors.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Boost enable_if library
  2. // Copyright 2003 (c) The Trustees of Indiana University.
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Authors: Jaakko Jarvi (jajarvi at osl.iu.edu)
  7. // Jeremiah Willcock (jewillco at osl.iu.edu)
  8. // Andrew Lumsdaine (lums at osl.iu.edu)
  9. #include <boost/utility/enable_if.hpp>
  10. #include <boost/type_traits.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. using boost::enable_if;
  13. using boost::disable_if;
  14. using boost::is_arithmetic;
  15. struct container {
  16. bool my_value;
  17. template <class T>
  18. container(const T&, const typename enable_if<is_arithmetic<T>, T>::type * = 0):
  19. my_value(true) {}
  20. template <class T>
  21. container(const T&, const typename disable_if<is_arithmetic<T>, T>::type * = 0):
  22. my_value(false) {}
  23. };
  24. // example from Howard Hinnant (tests enable_if template members of a templated class)
  25. template <class charT>
  26. struct xstring
  27. {
  28. template <class It>
  29. xstring(It begin, It end, typename
  30. disable_if<is_arithmetic<It> >::type* = 0)
  31. : data(end-begin) {}
  32. int data;
  33. };
  34. int main()
  35. {
  36. BOOST_TEST(container(1).my_value);
  37. BOOST_TEST(container(1.0).my_value);
  38. BOOST_TEST(!container("1").my_value);
  39. BOOST_TEST(!container(static_cast<void*>(0)).my_value);
  40. char sa[] = "123456";
  41. BOOST_TEST(xstring<char>(sa, sa+6).data == 6);
  42. return boost::report_errors();
  43. }