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

source code of boost/libs/pfr/include/boost/pfr/detail/core17.hpp