mp_defer.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2015-2017 Peter Dimov.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/mp11/utility.hpp>
  8. #include <boost/mp11/integral.hpp>
  9. #include <boost/mp11/detail/config.hpp>
  10. #include <boost/core/lightweight_test_trait.hpp>
  11. #include <type_traits>
  12. using boost::mp11::mp_identity;
  13. using boost::mp11::mp_true;
  14. using boost::mp11::mp_false;
  15. template<class T> struct has_type
  16. {
  17. template<class U> static mp_true f( mp_identity<typename U::type>* );
  18. template<class U> static mp_false f( ... );
  19. using type = decltype( f<T>(0) );
  20. static const bool value = type::value;
  21. };
  22. using boost::mp11::mp_defer;
  23. template<class T> using add_pointer = T*;
  24. template<class... T> using add_pointer_impl = mp_defer<add_pointer, T...>;
  25. using boost::mp11::mp_quote;
  26. using Q_add_pointer = mp_quote<add_pointer>;
  27. template<class... T> using Q_add_pointer_impl = mp_defer<Q_add_pointer::fn, T...>;
  28. int main()
  29. {
  30. BOOST_TEST_TRAIT_TRUE((has_type<add_pointer_impl<void>>));
  31. BOOST_TEST_TRAIT_TRUE((std::is_same<add_pointer_impl<void>::type, void*>));
  32. BOOST_TEST_TRAIT_TRUE((has_type<add_pointer_impl<int>>));
  33. BOOST_TEST_TRAIT_TRUE((std::is_same<add_pointer_impl<int>::type, int*>));
  34. BOOST_TEST_TRAIT_FALSE((has_type<add_pointer_impl<>>));
  35. BOOST_TEST_TRAIT_FALSE((has_type<add_pointer_impl<void, void>>));
  36. #if !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  37. BOOST_TEST_TRAIT_TRUE((has_type<Q_add_pointer_impl<void>>));
  38. #endif
  39. BOOST_TEST_TRAIT_TRUE((std::is_same<Q_add_pointer_impl<void>::type, void*>));
  40. #if !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  41. BOOST_TEST_TRAIT_TRUE((has_type<Q_add_pointer_impl<int>>));
  42. #endif
  43. BOOST_TEST_TRAIT_TRUE((std::is_same<Q_add_pointer_impl<int>::type, int*>));
  44. BOOST_TEST_TRAIT_FALSE((has_type<Q_add_pointer_impl<>>));
  45. BOOST_TEST_TRAIT_FALSE((has_type<Q_add_pointer_impl<void, void>>));
  46. return boost::report_errors();
  47. }