eif_lazy_test.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Testing all variations of lazy_enable_if.
  10. #include <boost/utility/enable_if.hpp>
  11. #include <boost/type_traits/is_same.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. using boost::lazy_enable_if;
  14. using boost::lazy_disable_if;
  15. using boost::lazy_enable_if_c;
  16. using boost::lazy_disable_if_c;
  17. template <class T>
  18. struct is_int_or_double {
  19. BOOST_STATIC_CONSTANT(bool,
  20. value = (boost::is_same<T, int>::value ||
  21. boost::is_same<T, double>::value));
  22. };
  23. template <class T>
  24. struct some_traits {
  25. typedef typename T::does_not_exist type;
  26. };
  27. template <>
  28. struct some_traits<int> {
  29. typedef bool type;
  30. };
  31. template <>
  32. struct some_traits<double> {
  33. typedef bool type;
  34. };
  35. template <class T>
  36. struct make_bool {
  37. typedef bool type;
  38. };
  39. template <>
  40. struct make_bool<int> {};
  41. template <>
  42. struct make_bool<double> {};
  43. namespace A {
  44. template<class T>
  45. typename lazy_enable_if<is_int_or_double<T>, some_traits<T> >::type
  46. foo(T t) { return true; }
  47. template<class T>
  48. typename lazy_enable_if_c<is_int_or_double<T>::value, some_traits<T> >::type
  49. foo2(T t) { return true; }
  50. }
  51. namespace B {
  52. template<class T>
  53. typename lazy_disable_if<is_int_or_double<T>, make_bool<T> >::type
  54. foo(T t) { return false; }
  55. template<class T>
  56. typename lazy_disable_if_c<is_int_or_double<T>::value, make_bool<T> >::type
  57. foo2(T t) { return false; }
  58. }
  59. int main()
  60. {
  61. using namespace A;
  62. using namespace B;
  63. BOOST_TEST(foo(1));
  64. BOOST_TEST(foo(1.0));
  65. BOOST_TEST(!foo("1"));
  66. BOOST_TEST(!foo(static_cast<void*>(0)));
  67. BOOST_TEST(foo2(1));
  68. BOOST_TEST(foo2(1.0));
  69. BOOST_TEST(!foo2("1"));
  70. BOOST_TEST(!foo2(static_cast<void*>(0)));
  71. return boost::report_errors();
  72. }