core14_loophole.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (c) 2017-2018 Alexandr Poltavsky, Antony Polukhin.
  2. // Copyright (c) 2019-2024 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // The Great Type Loophole (C++14)
  7. // Initial implementation by Alexandr Poltavsky, http://alexpolt.github.io
  8. //
  9. // Description:
  10. // The Great Type Loophole is a technique that allows to exchange type information with template
  11. // instantiations. Basically you can assign and read type information during compile time.
  12. // Here it is used to detect data members of a data type. I described it for the first time in
  13. // this blog post http://alexpolt.github.io/type-loophole.html .
  14. //
  15. // This technique exploits the http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#2118
  16. // CWG 2118. Stateful metaprogramming via friend injection
  17. // Note: CWG agreed that such techniques should be ill-formed, although the mechanism for prohibiting them is as yet undetermined.
  18. #ifndef BOOST_PFR_DETAIL_CORE14_LOOPHOLE_HPP
  19. #define BOOST_PFR_DETAIL_CORE14_LOOPHOLE_HPP
  20. #pragma once
  21. #include <boost/pfr/detail/config.hpp>
  22. #include <type_traits>
  23. #include <utility>
  24. #include <boost/pfr/detail/offset_based_getter.hpp>
  25. #include <boost/pfr/detail/fields_count.hpp>
  26. #include <boost/pfr/detail/make_flat_tuple_of_references.hpp>
  27. #include <boost/pfr/detail/make_integer_sequence.hpp>
  28. #include <boost/pfr/detail/sequence_tuple.hpp>
  29. #include <boost/pfr/detail/rvalue_t.hpp>
  30. #include <boost/pfr/detail/unsafe_declval.hpp>
  31. #ifdef __clang__
  32. # pragma clang diagnostic push
  33. # pragma clang diagnostic ignored "-Wmissing-braces"
  34. # pragma clang diagnostic ignored "-Wundefined-inline"
  35. # pragma clang diagnostic ignored "-Wundefined-internal"
  36. # pragma clang diagnostic ignored "-Wmissing-field-initializers"
  37. #elif defined(__GNUC__)
  38. # pragma GCC diagnostic push
  39. # pragma GCC diagnostic ignored "-Wnon-template-friend"
  40. #endif
  41. namespace boost { namespace pfr { namespace detail {
  42. // tag<T,N> generates friend declarations and helps with overload resolution.
  43. // There are two types: one with the auto return type, which is the way we read types later.
  44. // The second one is used in the detection of instantiations without which we'd get multiple
  45. // definitions.
  46. template <class T, std::size_t N>
  47. struct tag {
  48. friend auto loophole(tag<T,N>);
  49. };
  50. // The definitions of friend functions.
  51. template <class T, class U, std::size_t N, bool B>
  52. struct fn_def_lref {
  53. friend auto loophole(tag<T,N>) {
  54. // Standard Library containers do not SFINAE on invalid copy constructor. Because of that std::vector<std::unique_ptr<int>> reports that it is copyable,
  55. // which leads to an instantiation error at this place.
  56. //
  57. // To workaround the issue, we check that the type U is movable, and move it in that case.
  58. using no_extents_t = std::remove_all_extents_t<U>;
  59. return static_cast< std::conditional_t<std::is_move_constructible<no_extents_t>::value, no_extents_t&&, no_extents_t&> >(
  60. boost::pfr::detail::unsafe_declval<no_extents_t&>()
  61. );
  62. }
  63. };
  64. template <class T, class U, std::size_t N, bool B>
  65. struct fn_def_rref {
  66. friend auto loophole(tag<T,N>) { return std::move(boost::pfr::detail::unsafe_declval< std::remove_all_extents_t<U>& >()); }
  67. };
  68. // Those specializations are to avoid multiple definition errors.
  69. template <class T, class U, std::size_t N>
  70. struct fn_def_lref<T, U, N, true> {};
  71. template <class T, class U, std::size_t N>
  72. struct fn_def_rref<T, U, N, true> {};
  73. // This has a templated conversion operator which in turn triggers instantiations.
  74. // Important point, using sizeof seems to be more reliable. Also default template
  75. // arguments are "cached" (I think). To fix that I provide a U template parameter to
  76. // the ins functions which do the detection using constexpr friend functions and SFINAE.
  77. template <class T, std::size_t N>
  78. struct loophole_ubiq_lref {
  79. template<class U, std::size_t M> static std::size_t ins(...);
  80. template<class U, std::size_t M, std::size_t = sizeof(loophole(tag<T,M>{})) > static char ins(int);
  81. template<class U, std::size_t = sizeof(fn_def_lref<T, U, N, sizeof(ins<U, N>(0)) == sizeof(char)>)>
  82. constexpr operator U&() const&& noexcept; // `const&&` here helps to avoid ambiguity in loophole instantiations. optional_like test validate that behavior.
  83. };
  84. template <class T, std::size_t N>
  85. struct loophole_ubiq_rref {
  86. template<class U, std::size_t M> static std::size_t ins(...);
  87. template<class U, std::size_t M, std::size_t = sizeof(loophole(tag<T,M>{})) > static char ins(int);
  88. template<class U, std::size_t = sizeof(fn_def_rref<T, U, N, sizeof(ins<U, N>(0)) == sizeof(char)>)>
  89. constexpr operator U&&() const&& noexcept; // `const&&` here helps to avoid ambiguity in loophole instantiations. optional_like test validate that behavior.
  90. };
  91. // This is a helper to turn a data structure into a tuple.
  92. template <class T, class U>
  93. struct loophole_type_list_lref;
  94. template <typename T, std::size_t... I>
  95. struct loophole_type_list_lref< T, std::index_sequence<I...> >
  96. // Instantiating loopholes:
  97. : sequence_tuple::tuple< decltype(T{ loophole_ubiq_lref<T, I>{}... }, 0) >
  98. {
  99. using type = sequence_tuple::tuple< decltype(loophole(tag<T, I>{}))... >;
  100. };
  101. template <class T, class U>
  102. struct loophole_type_list_rref;
  103. template <typename T, std::size_t... I>
  104. struct loophole_type_list_rref< T, std::index_sequence<I...> >
  105. // Instantiating loopholes:
  106. : sequence_tuple::tuple< decltype(T{ loophole_ubiq_rref<T, I>{}... }, 0) >
  107. {
  108. using type = sequence_tuple::tuple< decltype(loophole(tag<T, I>{}))... >;
  109. };
  110. // Lazily returns loophole_type_list_{lr}ref.
  111. template <bool IsCopyConstructible /*= true*/, class T, class U>
  112. struct loophole_type_list_selector {
  113. using type = loophole_type_list_lref<T, U>;
  114. };
  115. template <class T, class U>
  116. struct loophole_type_list_selector<false /*IsCopyConstructible*/, T, U> {
  117. using type = loophole_type_list_rref<T, U>;
  118. };
  119. template <class T>
  120. auto tie_as_tuple_loophole_impl(T& lvalue) noexcept {
  121. using type = std::remove_cv_t<std::remove_reference_t<T>>;
  122. using indexes = detail::make_index_sequence<fields_count<type>()>;
  123. using loophole_type_list = typename detail::loophole_type_list_selector<
  124. std::is_copy_constructible<std::remove_all_extents_t<type>>::value, type, indexes
  125. >::type;
  126. using tuple_type = typename loophole_type_list::type;
  127. return boost::pfr::detail::make_flat_tuple_of_references(
  128. lvalue,
  129. offset_based_getter<type, tuple_type>{},
  130. size_t_<0>{},
  131. size_t_<tuple_type::size_v>{}
  132. );
  133. }
  134. template <class T>
  135. auto tie_as_tuple(T& val) noexcept {
  136. static_assert(
  137. !std::is_union<T>::value,
  138. "====================> Boost.PFR: For safety reasons it is forbidden to reflect unions. See `Reflection of unions` section in the docs for more info."
  139. );
  140. return boost::pfr::detail::tie_as_tuple_loophole_impl(
  141. val
  142. );
  143. }
  144. template <class T, class F, std::size_t... I>
  145. void for_each_field_dispatcher(T& t, F&& f, std::index_sequence<I...>) {
  146. static_assert(
  147. !std::is_union<T>::value,
  148. "====================> Boost.PFR: For safety reasons it is forbidden to reflect unions. See `Reflection of unions` section in the docs for more info."
  149. );
  150. std::forward<F>(f)(
  151. boost::pfr::detail::tie_as_tuple_loophole_impl(t)
  152. );
  153. }
  154. }}} // namespace boost::pfr::detail
  155. #ifdef __clang__
  156. # pragma clang diagnostic pop
  157. #elif defined(__GNUC__)
  158. # pragma GCC diagnostic pop
  159. #endif
  160. #endif // BOOST_PFR_DETAIL_CORE14_LOOPHOLE_HPP