traits.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. constexpr auto is_pod = detail::hana_trait<std::is_pod>{};
  62. constexpr auto is_literal_type = detail::hana_trait<std::is_literal_type>{};
  63. constexpr auto is_empty = detail::hana_trait<std::is_empty>{};
  64. constexpr auto is_polymorphic = detail::hana_trait<std::is_polymorphic>{};
  65. constexpr auto is_abstract = detail::hana_trait<std::is_abstract>{};
  66. constexpr auto is_signed = detail::hana_trait<std::is_signed>{};
  67. constexpr auto is_unsigned = detail::hana_trait<std::is_unsigned>{};
  68. // Supported operations
  69. constexpr auto is_constructible = detail::hana_trait<std::is_constructible>{};
  70. constexpr auto is_trivially_constructible = detail::hana_trait<std::is_trivially_constructible>{};
  71. constexpr auto is_nothrow_constructible = detail::hana_trait<std::is_nothrow_constructible>{};
  72. constexpr auto is_default_constructible = detail::hana_trait<std::is_default_constructible>{};
  73. constexpr auto is_trivially_default_constructible = detail::hana_trait<std::is_trivially_default_constructible>{};
  74. constexpr auto is_nothrow_default_constructible = detail::hana_trait<std::is_nothrow_default_constructible>{};
  75. constexpr auto is_copy_constructible = detail::hana_trait<std::is_copy_constructible>{};
  76. constexpr auto is_trivially_copy_constructible = detail::hana_trait<std::is_trivially_copy_constructible>{};
  77. constexpr auto is_nothrow_copy_constructible = detail::hana_trait<std::is_nothrow_copy_constructible>{};
  78. constexpr auto is_move_constructible = detail::hana_trait<std::is_move_constructible>{};
  79. constexpr auto is_trivially_move_constructible = detail::hana_trait<std::is_trivially_move_constructible>{};
  80. constexpr auto is_nothrow_move_constructible = detail::hana_trait<std::is_nothrow_move_constructible>{};
  81. constexpr auto is_assignable = detail::hana_trait<std::is_assignable>{};
  82. constexpr auto is_trivially_assignable = detail::hana_trait<std::is_trivially_assignable>{};
  83. constexpr auto is_nothrow_assignable = detail::hana_trait<std::is_nothrow_assignable>{};
  84. constexpr auto is_copy_assignable = detail::hana_trait<std::is_copy_assignable>{};
  85. constexpr auto is_trivially_copy_assignable = detail::hana_trait<std::is_trivially_copy_assignable>{};
  86. constexpr auto is_nothrow_copy_assignable = detail::hana_trait<std::is_nothrow_copy_assignable>{};
  87. constexpr auto is_move_assignable = detail::hana_trait<std::is_move_assignable>{};
  88. constexpr auto is_trivially_move_assignable = detail::hana_trait<std::is_trivially_move_assignable>{};
  89. constexpr auto is_nothrow_move_assignable = detail::hana_trait<std::is_nothrow_move_assignable>{};
  90. constexpr auto is_destructible = detail::hana_trait<std::is_destructible>{};
  91. constexpr auto is_trivially_destructible = detail::hana_trait<std::is_trivially_destructible>{};
  92. constexpr auto is_nothrow_destructible = detail::hana_trait<std::is_nothrow_destructible>{};
  93. constexpr auto has_virtual_destructor = detail::hana_trait<std::has_virtual_destructor>{};
  94. // Property queries
  95. constexpr auto alignment_of = detail::hana_trait<std::alignment_of>{};
  96. constexpr auto rank = detail::hana_trait<std::rank>{};
  97. constexpr struct extent_t {
  98. template <typename T, typename N>
  99. constexpr auto operator()(T const&, N const&) const {
  100. constexpr unsigned n = N::value;
  101. using Result = typename std::extent<typename T::type, n>::type;
  102. return hana::integral_c<typename Result::value_type, Result::value>;
  103. }
  104. template <typename T>
  105. constexpr auto operator()(T const& t) const
  106. { return (*this)(t, hana::uint_c<0>); }
  107. } extent{};
  108. // Type relationships
  109. constexpr auto is_same = detail::hana_trait<std::is_same>{};
  110. constexpr auto is_base_of = detail::hana_trait<std::is_base_of>{};
  111. constexpr auto is_convertible = detail::hana_trait<std::is_convertible>{};
  112. ///////////////////////
  113. // Type modifications
  114. ///////////////////////
  115. // Const-volatility specifiers
  116. constexpr auto remove_cv = metafunction<std::remove_cv>;
  117. constexpr auto remove_const = metafunction<std::remove_const>;
  118. constexpr auto remove_volatile = metafunction<std::remove_volatile>;
  119. constexpr auto add_cv = metafunction<std::add_cv>;
  120. constexpr auto add_const = metafunction<std::add_const>;
  121. constexpr auto add_volatile = metafunction<std::add_volatile>;
  122. // References
  123. constexpr auto remove_reference = metafunction<std::remove_reference>;
  124. constexpr auto add_lvalue_reference = metafunction<std::add_lvalue_reference>;
  125. constexpr auto add_rvalue_reference = metafunction<std::add_rvalue_reference>;
  126. // Pointers
  127. constexpr auto remove_pointer = metafunction<std::remove_pointer>;
  128. constexpr auto add_pointer = metafunction<std::add_pointer>;
  129. // Sign modifiers
  130. constexpr auto make_signed = metafunction<std::make_signed>;
  131. constexpr auto make_unsigned = metafunction<std::make_unsigned>;
  132. // Arrays
  133. constexpr auto remove_extent = metafunction<std::remove_extent>;
  134. constexpr auto remove_all_extents = metafunction<std::remove_all_extents>;
  135. // Miscellaneous transformations
  136. constexpr struct aligned_storage_t {
  137. template <typename Len, typename Align>
  138. constexpr auto operator()(Len const&, Align const&) const {
  139. constexpr std::size_t len = Len::value;
  140. constexpr std::size_t align = Align::value;
  141. using Result = typename std::aligned_storage<len, align>::type;
  142. return hana::type_c<Result>;
  143. }
  144. template <typename Len>
  145. constexpr auto operator()(Len const&) const {
  146. constexpr std::size_t len = Len::value;
  147. using Result = typename std::aligned_storage<len>::type;
  148. return hana::type_c<Result>;
  149. }
  150. } aligned_storage{};
  151. constexpr struct aligned_union_t {
  152. template <typename Len, typename ...T>
  153. constexpr auto operator()(Len const&, T const&...) const {
  154. constexpr std::size_t len = Len::value;
  155. using Result = typename std::aligned_union<len, typename T::type...>::type;
  156. return hana::type_c<Result>;
  157. }
  158. } aligned_union{};
  159. constexpr auto decay = metafunction<std::decay>;
  160. // enable_if
  161. // disable_if
  162. // conditional
  163. constexpr auto common_type = metafunction<std::common_type>;
  164. constexpr auto underlying_type = metafunction<std::underlying_type>;
  165. constexpr auto result_of = metafunction<std::result_of>;
  166. ///////////////////////
  167. // Utilities
  168. ///////////////////////
  169. struct declval_t {
  170. template <typename T>
  171. typename std::add_rvalue_reference<
  172. typename T::type
  173. >::type operator()(T const&) const;
  174. };
  175. constexpr declval_t declval{};
  176. } BOOST_HANA_NAMESPACE_END
  177. #endif // !BOOST_HANA_TRAITS_HPP