sfinae_friendly_metafunctions.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/equal.hpp>
  6. #include <boost/hana/not.hpp>
  7. #include <boost/hana/optional.hpp>
  8. #include <boost/hana/traits.hpp>
  9. #include <boost/hana/type.hpp>
  10. #include <type_traits>
  11. #include <utility>
  12. namespace hana = boost::hana;
  13. template <typename ...>
  14. using void_t = void;
  15. template <typename T, typename = void>
  16. struct has_type : std::false_type { };
  17. template <typename T>
  18. struct has_type<T, void_t<typename T::type>>
  19. : std::true_type
  20. { };
  21. auto common_type_impl = hana::sfinae([](auto t, auto u) -> hana::type<
  22. decltype(true ? hana::traits::declval(t) : hana::traits::declval(u))
  23. > { return {}; });
  24. template <typename T, typename U>
  25. using common_type = decltype(common_type_impl(hana::type_c<T>, hana::type_c<U>));
  26. BOOST_HANA_CONSTANT_CHECK(
  27. common_type_impl(hana::type_c<int>, hana::type_c<float>)
  28. ==
  29. hana::just(hana::type_c<float>)
  30. );
  31. static_assert(!has_type<common_type<int, int*>>{}, "");
  32. static_assert(std::is_same<common_type<int, float>::type, float>{}, "");
  33. int main() { }