core17.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2016-2024 Antony Polukhin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PFR_DETAIL_CORE17_HPP
  6. #define BOOST_PFR_DETAIL_CORE17_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/core17_generated.hpp>
  9. #include <boost/pfr/detail/fields_count.hpp>
  10. #include <boost/pfr/detail/for_each_field_impl.hpp>
  11. #include <boost/pfr/detail/rvalue_t.hpp>
  12. namespace boost { namespace pfr { namespace detail {
  13. #ifndef _MSC_VER // MSVC fails to compile the following code, but compiles the structured bindings in core17_generated.hpp
  14. struct do_not_define_std_tuple_size_for_me {
  15. bool test1 = true;
  16. };
  17. template <class T>
  18. constexpr bool do_structured_bindings_work() noexcept { // ******************************************* IN CASE OF ERROR READ THE FOLLOWING LINES IN boost/pfr/detail/core17.hpp FILE:
  19. T val{};
  20. auto& [a] = val; // ******************************************* IN CASE OF ERROR READ THE FOLLOWING LINES IN boost/pfr/detail/core17.hpp FILE:
  21. /****************************************************************************
  22. *
  23. * It looks like your compiler or Standard Library can not handle C++17
  24. * structured bindings.
  25. *
  26. * Workaround: Define BOOST_PFR_USE_CPP17 to 0
  27. * It will disable the C++17 features for Boost.PFR library.
  28. *
  29. * Sorry for the inconvenience caused.
  30. *
  31. ****************************************************************************/
  32. return a;
  33. }
  34. static_assert(
  35. do_structured_bindings_work<do_not_define_std_tuple_size_for_me>(),
  36. "====================> Boost.PFR: Your compiler can not handle C++17 structured bindings. Read the above comments for workarounds."
  37. );
  38. #endif // #ifndef _MSC_VER
  39. template <class T>
  40. constexpr auto tie_as_tuple(T& val) noexcept {
  41. static_assert(
  42. !std::is_union<T>::value,
  43. "====================> Boost.PFR: For safety reasons it is forbidden to reflect unions. See `Reflection of unions` section in the docs for more info."
  44. );
  45. typedef size_t_<boost::pfr::detail::fields_count<T>()> fields_count_tag;
  46. return boost::pfr::detail::tie_as_tuple(val, fields_count_tag{});
  47. }
  48. template <class T, class F, std::size_t... I>
  49. constexpr void for_each_field_dispatcher(T& t, F&& f, std::index_sequence<I...>) {
  50. static_assert(
  51. !std::is_union<T>::value,
  52. "====================> Boost.PFR: For safety reasons it is forbidden to reflect unions. See `Reflection of unions` section in the docs for more info."
  53. );
  54. std::forward<F>(f)(
  55. detail::tie_as_tuple(t)
  56. );
  57. }
  58. }}} // namespace boost::pfr::detail
  59. #endif // BOOST_PFR_DETAIL_CORE17_HPP