traits.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*!
  2. @file
  3. Defines function-like equivalents to the standard `<type_traits>`, and also
  4. to some utilities like `std::declval`.
  5. @copyright Louis Dionne 2013-2017
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  8. */
  9. #ifndef BOOST_HANA_TRAITS_HPP
  10. #define BOOST_HANA_TRAITS_HPP
  11. #include <boost/hana/config.hpp>
  12. #include <boost/hana/integral_constant.hpp>
  13. #include <boost/hana/type.hpp>
  14. #include <cstddef>
  15. #include <type_traits>
  16. BOOST_HANA_NAMESPACE_BEGIN namespace traits {
  17. namespace detail {
  18. // We use this instead of hana::integral because we want to return
  19. // hana::integral_constants instead of std::integral_constants.
  20. template <template <typename ...> class F>
  21. struct hana_trait {
  22. template <typename ...T>
  23. constexpr auto operator()(T const& ...) const {
  24. using Result = typename F<typename T::type...>::type;
  25. return hana::integral_c<typename Result::value_type, Result::value>;
  26. }
  27. };
  28. }
  29. ///////////////////////
  30. // Type properties
  31. ///////////////////////
  32. // Primary type categories
  33. constexpr auto is_void = detail::hana_trait<std::is_void>{};
  34. constexpr auto is_null_pointer = detail::hana_trait<std::is_null_pointer>{};
  35. constexpr auto is_integral = detail::hana_trait<std::is_integral>{};
  36. constexpr auto is_floating_point = detail::hana_trait<std::is_floating_point>{};
  37. constexpr auto is_array = detail::hana_trait<std::is_array>{};
  38. constexpr auto is_enum = detail::hana_trait<std::is_enum>{};
  39. constexpr auto is_union = detail::hana_trait<std::is_union>{};
  40. constexpr auto is_class = detail::hana_trait<std::is_class>{};
  41. constexpr auto is_function = detail::hana_trait<std::is_function>{};
  42. constexpr auto is_pointer = detail::hana_trait<std::is_pointer>{};
  43. constexpr auto is_lvalue_reference = detail::hana_trait<std::is_lvalue_reference>{};
  44. constexpr auto is_rvalue_reference = detail::hana_trait<std::is_rvalue_reference>{};
  45. constexpr auto is_member_object_pointer = detail::hana_trait<std::is_member_object_pointer>{};
  46. constexpr auto is_member_function_pointer = detail::hana_trait<std::is_member_function_pointer>{};
  47. // Composite type categories
  48. constexpr auto is_fundamental = detail::hana_trait<std::is_fundamental>{};
  49. constexpr auto is_arithmetic = detail::hana_trait<std::is_arithmetic>{};
  50. constexpr auto is_scalar = detail::hana_trait<std::is_scalar>{};
  51. constexpr auto is_object = detail::hana_trait<std::is_object>{};
  52. constexpr auto is_compound = detail::hana_trait<std::is_compound>{};
  53. constexpr auto is_reference = detail::hana_trait<std::is_reference>{};
  54. constexpr auto is_member_pointer = detail::hana_trait<std::is_member_pointer>{};
  55. // Type properties
  56. constexpr auto is_const = detail::hana_trait<std::is_const>{};
  57. constexpr auto is_volatile = detail::hana_trait<std::is_volatile>{};
  58. constexpr auto is_trivial = detail::hana_trait<std::is_trivial>{};
  59. constexpr auto is_trivially_copyable = detail::hana_trait<std::is_trivially_copyable>{};
  60. constexpr auto is_standard_layout = detail::hana_trait<std::is_standard_layout>{};
  61. #if __cplusplus < 202002L
  62. constexpr auto is_pod = detail::hana_trait<std::is_pod>{};
  63. #endif
  64. constexpr auto is_literal_type = detail::hana_trait<std::is_literal_type>{};
  65. constexpr auto is_empty = detail::hana_trait<std::is_empty>{};
  66. constexpr auto is_polymorphic = detail::hana_trait<std::is_polymorphic>{};
  67. constexpr auto is_abstract = detail::hana_trait<std::is_abstract>{};
  68. constexpr auto is_signed = detail::hana_trait<std::is_signed>{};
  69. constexpr auto is_unsigned = detail::hana_trait<std::is_unsigned>{};
  70. // Supported operations
  71. constexpr auto is_constructible = detail::hana_trait<std::is_constructible>{};
  72. constexpr auto is_trivially_constructible = detail::hana_trait<std::is_trivially_constructible>{};
  73. constexpr auto is_nothrow_constructible = detail::hana_trait<std::is_nothrow_constructible>{};
  74. constexpr auto is_default_constructible = detail::hana_trait<std::is_default_constructible>{};
  75. constexpr auto is_trivially_default_constructible = detail::hana_trait<std::is_trivially_default_constructible>{};
  76. constexpr auto is_nothrow_default_constructible = detail::hana_trait<std::is_nothrow_default_constructible>{};
  77. constexpr auto is_copy_constructible = detail::hana_trait<std::is_copy_constructible>{};
  78. constexpr auto is_trivially_copy_constructible = detail::hana_trait<std::is_trivially_copy_constructible>{};
  79. constexpr auto is_nothrow_copy_constructible = detail::hana_trait<std::is_nothrow_copy_constructible>{};
  80. constexpr auto is_move_constructible = detail::hana_trait<std::is_move_constructible>{};
  81. constexpr auto is_trivially_move_constructible = detail::hana_trait<std::is_trivially_move_constructible>{};
  82. constexpr auto is_nothrow_move_constructible = detail::hana_trait<std::is_nothrow_move_constructible>{};
  83. constexpr auto is_assignable = detail::hana_trait<std::is_assignable>{};
  84. constexpr auto is_trivially_assignable = detail::hana_trait<std::is_trivially_assignable>{};
  85. constexpr auto is_nothrow_assignable = detail::hana_trait<std::is_nothrow_assignable>{};
  86. constexpr auto is_copy_assignable = detail::hana_trait<std::is_copy_assignable>{};
  87. constexpr auto is_trivially_copy_assignable = detail::hana_trait<std::is_trivially_copy_assignable>{};
  88. constexpr auto is_nothrow_copy_assignable = detail::hana_trait<std::is_nothrow_copy_assignable>{};
  89. constexpr auto is_move_assignable = detail::hana_trait<std::is_move_assignable>{};
  90. constexpr auto is_trivially_move_assignable = detail::hana_trait<std::is_trivially_move_assignable>{};
  91. constexpr auto is_nothrow_move_assignable = detail::hana_trait<std::is_nothrow_move_assignable>{};
  92. constexpr auto is_destructible = detail::hana_trait<std::is_destructible>{};
  93. constexpr auto is_trivially_destructible = detail::hana_trait<std::is_trivially_destructible>{};
  94. constexpr auto is_nothrow_destructible = detail::hana_trait<std::is_nothrow_destructible>{};
  95. constexpr auto has_virtual_destructor = detail::hana_trait<std::has_virtual_destructor>{};
  96. // Property queries
  97. constexpr auto alignment_of = detail::hana_trait<std::alignment_of>{};
  98. constexpr auto rank = detail::hana_trait<std::rank>{};
  99. constexpr struct extent_t {
  100. template <typename T, typename N>
  101. constexpr auto operator()(T const&, N const&) const {
  102. constexpr unsigned n = N::value;
  103. using Result = typename std::extent<typename T::type, n>::type;
  104. return hana::integral_c<typename Result::value_type, Result::value>;
  105. }
  106. template <typename T>
  107. constexpr auto operator()(T const& t) const
  108. { return (*this)(t, hana::uint_c<0>); }
  109. } extent{};
  110. // Type relationships
  111. constexpr auto is_same = detail::hana_trait<std::is_same>{};
  112. constexpr auto is_base_of = detail::hana_trait<std::is_base_of>{};
  113. constexpr auto is_convertible = detail::hana_trait<std::is_convertible>{};
  114. ///////////////////////
  115. // Type modifications
  116. ///////////////////////
  117. // Const-volatility specifiers
  118. constexpr auto remove_cv = metafunction<std::remove_cv>;
  119. constexpr auto remove_const = metafunction<std::remove_const>;
  120. constexpr auto remove_volatile = metafunction<std::remove_volatile>;
  121. constexpr auto add_cv = metafunction<std::add_cv>;
  122. constexpr auto add_const = metafunction<std::add_const>;
  123. constexpr auto add_volatile = metafunction<std::add_volatile>;
  124. // References
  125. constexpr auto remove_reference = metafunction<std::remove_reference>;
  126. constexpr auto add_lvalue_reference = metafunction<std::add_lvalue_reference>;
  127. constexpr auto add_rvalue_reference = metafunction<std::add_rvalue_reference>;
  128. // Pointers
  129. constexpr auto remove_pointer = metafunction<std::remove_pointer>;
  130. constexpr auto add_pointer = metafunction<std::add_pointer>;
  131. // Sign modifiers
  132. constexpr auto make_signed = metafunction<std::make_signed>;
  133. constexpr auto make_unsigned = metafunction<std::make_unsigned>;
  134. // Arrays
  135. constexpr auto remove_extent = metafunction<std::remove_extent>;
  136. constexpr auto remove_all_extents = metafunction<std::remove_all_extents>;
  137. // Miscellaneous transformations
  138. constexpr struct aligned_storage_t {
  139. template <typename Len, typename Align>
  140. constexpr auto operator()(Len const&, Align const&) const {
  141. constexpr std::size_t len = Len::value;
  142. constexpr std::size_t align = Align::value;
  143. using Result = typename std::aligned_storage<len, align>::type;
  144. return hana::type_c<Result>;
  145. }
  146. template <typename Len>
  147. constexpr auto operator()(Len const&) const {
  148. constexpr std::size_t len = Len::value;
  149. using Result = typename std::aligned_storage<len>::type;
  150. return hana::type_c<Result>;
  151. }
  152. } aligned_storage{};
  153. constexpr struct aligned_union_t {
  154. template <typename Len, typename ...T>
  155. constexpr auto operator()(Len const&, T const&...) const {
  156. constexpr std::size_t len = Len::value;
  157. using Result = typename std::aligned_union<len, typename T::type...>::type;
  158. return hana::type_c<Result>;
  159. }
  160. } aligned_union{};
  161. constexpr auto decay = metafunction<std::decay>;
  162. // enable_if
  163. // disable_if
  164. // conditional
  165. constexpr auto common_type = metafunction<std::common_type>;
  166. constexpr auto underlying_type = metafunction<std::underlying_type>;
  167. ///////////////////////
  168. // Utilities
  169. ///////////////////////
  170. struct declval_t {
  171. template <typename T>
  172. typename std::add_rvalue_reference<
  173. typename T::type
  174. >::type operator()(T const&) const;
  175. };
  176. constexpr declval_t declval{};
  177. } BOOST_HANA_NAMESPACE_END
  178. #endif // !BOOST_HANA_TRAITS_HPP