typeid.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/type.hpp>
  7. namespace hana = boost::hana;
  8. using Function = void();
  9. void function() { }
  10. int main() {
  11. {
  12. struct T { };
  13. T t;
  14. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  15. hana::typeid_(T{}),
  16. hana::type_c<T>
  17. ));
  18. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  19. hana::typeid_(t),
  20. hana::type_c<T>
  21. ));
  22. }
  23. // [cv-qualified] reference types
  24. {
  25. struct T { };
  26. T t;
  27. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  28. hana::typeid_(static_cast<T&>(t)),
  29. hana::type_c<T>
  30. ));
  31. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  32. hana::typeid_(static_cast<T const&>(t)),
  33. hana::type_c<T>
  34. ));
  35. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  36. hana::typeid_(static_cast<T volatile&>(t)),
  37. hana::type_c<T>
  38. ));
  39. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  40. hana::typeid_(static_cast<T const volatile&>(t)),
  41. hana::type_c<T>
  42. ));
  43. }
  44. // [cv-qualified] rvalue reference types
  45. {
  46. struct T { };
  47. T t;
  48. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  49. hana::typeid_(static_cast<T&&>(t)),
  50. hana::type_c<T>
  51. ));
  52. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  53. hana::typeid_(static_cast<T const &&>(t)),
  54. hana::type_c<T>
  55. ));
  56. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  57. hana::typeid_(static_cast<T volatile&&>(t)),
  58. hana::type_c<T>
  59. ));
  60. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  61. hana::typeid_(static_cast<T const volatile&&>(t)),
  62. hana::type_c<T>
  63. ));
  64. }
  65. // typeid_(type_c<T>) is the identity function
  66. {
  67. struct T;
  68. auto const type_const = hana::type_c<T>;
  69. auto const& type_const_ref = hana::type_c<T>;
  70. auto& type_ref = hana::type_c<T>;
  71. auto&& type_ref_ref = static_cast<decltype(type_ref)&&>(type_ref);
  72. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  73. hana::typeid_(hana::type_c<T>),
  74. hana::type_c<T>
  75. ));
  76. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  77. hana::typeid_(type_const),
  78. hana::type_c<T>
  79. ));
  80. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  81. hana::typeid_(type_const_ref),
  82. hana::type_c<T>
  83. ));
  84. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  85. hana::typeid_(type_ref),
  86. hana::type_c<T>
  87. ));
  88. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  89. hana::typeid_(type_ref_ref),
  90. hana::type_c<T>
  91. ));
  92. }
  93. // make sure we don't read from non-constexpr variables
  94. {
  95. struct T;
  96. auto t = hana::type_c<T>;
  97. auto x = 1;
  98. constexpr auto r1 = hana::typeid_(t); (void)r1;
  99. constexpr auto r2 = hana::typeid_(x); (void)r2;
  100. }
  101. // typeid_ with builtin arrays, function pointers and other weirdos
  102. {
  103. struct T { };
  104. using A = T[3];
  105. A a;
  106. A& a_ref = a;
  107. A const& a_const_ref = a;
  108. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  109. hana::typeid_(a),
  110. hana::type_c<A>
  111. ));
  112. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  113. hana::typeid_(a_ref),
  114. hana::type_c<A>
  115. ));
  116. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  117. hana::typeid_(a_const_ref),
  118. hana::type_c<A>
  119. ));
  120. }
  121. {
  122. using Fptr = int(*)();
  123. Fptr f;
  124. Fptr& f_ref = f;
  125. Fptr const& f_const_ref = f;
  126. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  127. hana::typeid_(f),
  128. hana::type_c<Fptr>
  129. ));
  130. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  131. hana::typeid_(f_ref),
  132. hana::type_c<Fptr>
  133. ));
  134. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  135. hana::typeid_(f_const_ref),
  136. hana::type_c<Fptr>
  137. ));
  138. }
  139. {
  140. Function& function_ref = function;
  141. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  142. hana::typeid_(function),
  143. hana::type_c<Function>
  144. ));
  145. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  146. hana::typeid_(function_ref),
  147. hana::type_c<Function>
  148. ));
  149. }
  150. }