optional_deduced_sfinae.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright Daniel Wallin 2006.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/parameter/config.hpp>
  6. #if (BOOST_PARAMETER_MAX_ARITY < 2)
  7. #error Define BOOST_PARAMETER_MAX_ARITY as 2 or greater.
  8. #endif
  9. #if !defined(BOOST_PARAMETER_HAS_PERFECT_FORWARDING) && \
  10. (BOOST_PARAMETER_EXPONENTIAL_OVERLOAD_THRESHOLD_ARITY < 2)
  11. #error Define BOOST_PARAMETER_EXPONENTIAL_OVERLOAD_THRESHOLD_ARITY \
  12. as 2 or greater.
  13. #endif
  14. #include <boost/parameter/preprocessor.hpp>
  15. #include <boost/parameter/name.hpp>
  16. #include <boost/parameter/aux_/preprocessor/nullptr.hpp>
  17. #include <boost/tuple/tuple.hpp>
  18. #include <boost/core/enable_if.hpp>
  19. #include <string>
  20. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  21. #include <boost/mp11/utility.hpp>
  22. #include <type_traits>
  23. #else
  24. #include <boost/mpl/bool.hpp>
  25. #include <boost/mpl/if.hpp>
  26. #include <boost/type_traits/is_convertible.hpp>
  27. #endif
  28. namespace test {
  29. BOOST_PARAMETER_NAME(x)
  30. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  31. template <typename T, typename Args>
  32. using predicate = std::is_convertible<T,char const*>;
  33. BOOST_PARAMETER_FUNCTION((int), sfinae, test::tag,
  34. (deduced
  35. (optional
  36. (x
  37. , *(boost::mp11::mp_quote<test::predicate>)
  38. , static_cast<char const*>(BOOST_PARAMETER_AUX_PP_NULLPTR)
  39. )
  40. )
  41. )
  42. )
  43. #else // !defined(BOOST_PARAMETER_CAN_USE_MP11)
  44. struct predicate
  45. {
  46. template <typename T, typename Args>
  47. struct apply
  48. : boost::mpl::if_<
  49. boost::is_convertible<T,char const*>
  50. , boost::mpl::true_
  51. , boost::mpl::false_
  52. >
  53. {
  54. };
  55. };
  56. BOOST_PARAMETER_FUNCTION((int), sfinae, test::tag,
  57. (deduced
  58. (optional
  59. (x
  60. , *(test::predicate)
  61. , static_cast<char const*>(BOOST_PARAMETER_AUX_PP_NULLPTR)
  62. )
  63. )
  64. )
  65. )
  66. #endif // BOOST_PARAMETER_CAN_USE_MP11
  67. {
  68. return 1;
  69. }
  70. template <typename A0>
  71. typename boost::enable_if<
  72. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  73. std::is_same<int,A0>
  74. #else
  75. typename boost::mpl::if_<
  76. boost::is_same<int,A0>
  77. , boost::mpl::true_
  78. , boost::mpl::false_
  79. >::type
  80. #endif
  81. , int
  82. >::type
  83. sfinae(A0 const& a0)
  84. {
  85. return 0;
  86. }
  87. } // namespace test
  88. #include <boost/core/lightweight_test.hpp>
  89. int main()
  90. {
  91. BOOST_TEST_EQ(1, test::sfinae());
  92. BOOST_TEST_EQ(1, test::sfinae("foo"));
  93. BOOST_TEST_EQ(0, test::sfinae(1));
  94. return boost::report_errors();
  95. }