tag_of.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/core/tag_of.hpp>
  5. #include <boost/hana/core/when.hpp>
  6. #include <type_traits>
  7. namespace hana = boost::hana;
  8. template <typename T, typename ExpectedDatatype>
  9. struct test {
  10. static_assert(std::is_same<hana::tag_of_t<T>, ExpectedDatatype>::value, "");
  11. static_assert(std::is_same<hana::tag_of_t<T const>, ExpectedDatatype>::value, "");
  12. static_assert(std::is_same<hana::tag_of_t<T volatile>, ExpectedDatatype>::value, "");
  13. static_assert(std::is_same<hana::tag_of_t<T const volatile>, ExpectedDatatype>::value, "");
  14. static_assert(std::is_same<hana::tag_of_t<T&>, ExpectedDatatype>::value, "");
  15. static_assert(std::is_same<hana::tag_of_t<T const&>, ExpectedDatatype>::value, "");
  16. static_assert(std::is_same<hana::tag_of_t<T volatile&>, ExpectedDatatype>::value, "");
  17. static_assert(std::is_same<hana::tag_of_t<T const volatile&>, ExpectedDatatype>::value, "");
  18. static_assert(std::is_same<hana::tag_of_t<T&&>, ExpectedDatatype>::value, "");
  19. static_assert(std::is_same<hana::tag_of_t<T const&&>, ExpectedDatatype>::value, "");
  20. static_assert(std::is_same<hana::tag_of_t<T volatile&&>, ExpectedDatatype>::value, "");
  21. static_assert(std::is_same<hana::tag_of_t<T const volatile&&>, ExpectedDatatype>::value, "");
  22. };
  23. struct NestedDatatype;
  24. struct Nested { struct hana_tag; };
  25. template struct test<Nested, Nested::hana_tag>;
  26. struct NoNestedDatatype { };
  27. template struct test<NoNestedDatatype, NoNestedDatatype>;
  28. struct NoNestedHana { };
  29. template struct test<NoNestedHana, NoNestedHana>;
  30. struct FullySpecializedDatatype;
  31. struct FullySpecialized;
  32. namespace boost { namespace hana {
  33. template <>
  34. struct tag_of<FullySpecialized> {
  35. using type = FullySpecializedDatatype;
  36. };
  37. }}
  38. template struct test<FullySpecialized, FullySpecializedDatatype>;
  39. struct PartiallySpecializedDatatype;
  40. template <typename> struct PartiallySpecialized;
  41. namespace boost { namespace hana {
  42. template <typename T>
  43. struct tag_of<PartiallySpecialized<T>> {
  44. using type = PartiallySpecializedDatatype;
  45. };
  46. }}
  47. template struct test<PartiallySpecialized<struct anything>, PartiallySpecializedDatatype>;
  48. struct PredicatedDatatype;
  49. struct Predicated { static constexpr bool predicate = true; };
  50. namespace boost { namespace hana {
  51. template <typename T>
  52. struct tag_of<T, hana::when<T::predicate>> {
  53. using type = PredicatedDatatype;
  54. };
  55. }}
  56. template struct test<Predicated, PredicatedDatatype>;
  57. int main() { }