Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:54:34

0001 // Copyright (c) 2016-2025 Antony Polukhin
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_PFR_DETAIL_FIELDS_COUNT_HPP
0007 #define BOOST_PFR_DETAIL_FIELDS_COUNT_HPP
0008 #pragma once
0009 
0010 #include <boost/pfr/detail/config.hpp>
0011 #include <boost/pfr/detail/make_integer_sequence.hpp>
0012 #include <boost/pfr/detail/size_t_.hpp>
0013 #include <boost/pfr/detail/unsafe_declval.hpp>
0014 
0015 #ifdef BOOST_PFR_HAS_STD_MODULE
0016 import std;
0017 #else
0018 #include <climits>      // CHAR_BIT
0019 #include <cstdint>      // SIZE_MAX
0020 #include <type_traits>
0021 #include <utility>      // metaprogramming stuff
0022 #endif
0023 
0024 #ifdef __clang__
0025 #   pragma clang diagnostic push
0026 #   pragma clang diagnostic ignored "-Wmissing-braces"
0027 #   pragma clang diagnostic ignored "-Wundefined-inline"
0028 #   pragma clang diagnostic ignored "-Wundefined-internal"
0029 #   pragma clang diagnostic ignored "-Wmissing-field-initializers"
0030 #endif
0031 
0032 namespace boost { namespace pfr { namespace detail {
0033 
0034 ///////////////////// min without including <algorithm>
0035 constexpr std::size_t min_of_size_t(std::size_t a, std::size_t b) noexcept {
0036     return b < a ? b : a;
0037 }
0038 
0039 ///////////////////// Structure that can be converted to reference to anything
0040 struct ubiq_lref_constructor {
0041     std::size_t ignore;
0042     template <class Type> constexpr operator Type&() const && noexcept {  // tweak for template_unconstrained.cpp like cases
0043         return detail::unsafe_declval<Type&>();
0044     }
0045 
0046     template <class Type> constexpr operator Type&() const & noexcept {  // tweak for optional_chrono.cpp like cases
0047         return detail::unsafe_declval<Type&>();
0048     }
0049 };
0050 
0051 ///////////////////// Structure that can be converted to rvalue reference to anything
0052 struct ubiq_rref_constructor {
0053     std::size_t ignore;
0054     template <class Type> /*constexpr*/ operator Type() const && noexcept {  // Allows initialization of rvalue reference fields and move-only types
0055         return detail::unsafe_declval<Type>();
0056     }
0057 };
0058 
0059 ///////////////////// Hand-made is_complete<T> trait
0060 template <typename T, typename = void>
0061 struct is_complete : std::false_type
0062 {};
0063 
0064 template <typename T>
0065 struct is_complete<T, decltype(void(sizeof(T)))> : std::integral_constant<bool, true>
0066 {};
0067 
0068 #ifndef __cpp_lib_is_aggregate
0069 ///////////////////// Hand-made is_aggregate_initializable_n<T> trait
0070 
0071 // Structure that can be converted to reference to anything except reference to T
0072 template <class T, bool IsCopyConstructible>
0073 struct ubiq_constructor_except {
0074     std::size_t ignore;
0075     template <class Type> constexpr operator std::enable_if_t<!std::is_same<T, Type>::value, Type&> () const noexcept; // Undefined
0076 };
0077 
0078 template <class T>
0079 struct ubiq_constructor_except<T, false> {
0080     std::size_t ignore;
0081     template <class Type> constexpr operator std::enable_if_t<!std::is_same<T, Type>::value, Type&&> () const noexcept; // Undefined
0082 };
0083 
0084 
0085 // `std::is_constructible<T, ubiq_constructor_except<T>>` consumes a lot of time, so we made a separate lazy trait for it.
0086 template <std::size_t N, class T> struct is_single_field_and_aggregate_initializable: std::false_type {};
0087 template <class T> struct is_single_field_and_aggregate_initializable<1, T>: std::integral_constant<
0088     bool, !std::is_constructible<T, ubiq_constructor_except<T, std::is_copy_constructible<T>::value>>::value
0089 > {};
0090 
0091 // Hand-made is_aggregate<T> trait:
0092 // Before C++20 aggregates could be constructed from `decltype(ubiq_?ref_constructor{I})...` but type traits report that
0093 // there's no constructor from `decltype(ubiq_?ref_constructor{I})...`
0094 // Special case for N == 1: `std::is_constructible<T, ubiq_?ref_constructor>` returns true if N == 1 and T is copy/move constructible.
0095 template <class T, std::size_t N, class /*Enable*/ = void>
0096 struct is_aggregate_initializable_n {
0097     static constexpr bool value =
0098            std::is_empty<T>::value
0099         || std::is_array<T>::value
0100     ;
0101 };
0102 
0103 template <class T, std::size_t N>
0104 struct is_aggregate_initializable_n<T, N, std::enable_if_t<std::is_class<T>::value && !std::is_empty<T>::value>> {
0105     template <std::size_t ...I>
0106     static constexpr bool is_not_constructible_n(std::index_sequence<I...>) noexcept {
0107         return (!std::is_constructible<T, decltype(ubiq_lref_constructor{I})...>::value && !std::is_constructible<T, decltype(ubiq_rref_constructor{I})...>::value)
0108             || is_single_field_and_aggregate_initializable<N, T>::value
0109         ;
0110     }
0111 
0112     static constexpr bool value = is_not_constructible_n(detail::make_index_sequence<N>{});
0113 };
0114 
0115 #endif // #ifndef __cpp_lib_is_aggregate
0116 
0117 ///////////////////// Detect aggregates with inheritance
0118 template <class Derived, class U>
0119 constexpr bool static_assert_non_inherited() noexcept {
0120     static_assert(
0121             !std::is_base_of<U, Derived>::value,
0122             "====================> Boost.PFR: Boost.PFR: Inherited types are not supported."
0123     );
0124     return true;
0125 }
0126 
0127 template <class Derived>
0128 struct ubiq_lref_base_asserting {
0129     template <class Type> constexpr operator Type&() const &&  // tweak for template_unconstrained.cpp like cases
0130         noexcept(detail::static_assert_non_inherited<Derived, Type>())  // force the computation of assert function
0131     {
0132         return detail::unsafe_declval<Type&>();
0133     }
0134 
0135     template <class Type> constexpr operator Type&() const &  // tweak for optional_chrono.cpp like cases
0136         noexcept(detail::static_assert_non_inherited<Derived, Type>())  // force the computation of assert function
0137     {
0138         return detail::unsafe_declval<Type&>();
0139     }
0140 };
0141 
0142 template <class Derived>
0143 struct ubiq_rref_base_asserting {
0144     template <class Type> /*constexpr*/ operator Type() const &&  // Allows initialization of rvalue reference fields and move-only types
0145         noexcept(detail::static_assert_non_inherited<Derived, Type>())  // force the computation of assert function
0146     {
0147         return detail::unsafe_declval<Type>();
0148     }
0149 };
0150 
0151 template <class T, std::size_t I0, std::size_t... I, class /*Enable*/ = std::enable_if_t<std::is_copy_constructible<T>::value>>
0152 constexpr auto assert_first_not_base(std::index_sequence<I0, I...>) noexcept
0153     -> std::add_pointer_t<decltype(T{ ubiq_lref_base_asserting<T>{}, ubiq_lref_constructor{I}... })>
0154 {
0155     return nullptr;
0156 }
0157 
0158 template <class T, std::size_t I0, std::size_t... I, class /*Enable*/ = std::enable_if_t<!std::is_copy_constructible<T>::value>>
0159 constexpr auto assert_first_not_base(std::index_sequence<I0, I...>) noexcept
0160     -> std::add_pointer_t<decltype(T{ ubiq_rref_base_asserting<T>{}, ubiq_rref_constructor{I}... })>
0161 {
0162     return nullptr;
0163 }
0164 
0165 template <class T>
0166 constexpr void* assert_first_not_base(std::index_sequence<>) noexcept
0167 {
0168     return nullptr;
0169 }
0170 
0171 template <class T, std::size_t N>
0172 constexpr void assert_first_not_base(int) noexcept {}
0173 
0174 template <class T, std::size_t N>
0175 constexpr auto assert_first_not_base(long) noexcept
0176     -> std::enable_if_t<std::is_class<T>::value>
0177 {
0178     detail::assert_first_not_base<T>(detail::make_index_sequence<N>{});
0179 }
0180 
0181 ///////////////////// Helpers for initializable detection
0182 // Note that these take O(N) compile time and memory!
0183 template <class T, std::size_t... I, class /*Enable*/ = std::enable_if_t<std::is_copy_constructible<T>::value>>
0184 constexpr auto enable_if_initializable_helper(std::index_sequence<I...>) noexcept
0185     -> std::add_pointer_t<decltype(T{ubiq_lref_constructor{I}...})>;
0186 
0187 template <class T, std::size_t... I, class /*Enable*/ = std::enable_if_t<!std::is_copy_constructible<T>::value>>
0188 constexpr auto enable_if_initializable_helper(std::index_sequence<I...>) noexcept
0189     -> std::add_pointer_t<decltype(T{ubiq_rref_constructor{I}...})>;
0190 
0191 template <class T, std::size_t N, class U = std::size_t, class /*Enable*/ = decltype(detail::enable_if_initializable_helper<T>(detail::make_index_sequence<N>()))>
0192 using enable_if_initializable_helper_t = U;
0193 
0194 template <class T, std::size_t N>
0195 constexpr auto is_initializable(long) noexcept
0196     -> detail::enable_if_initializable_helper_t<T, N, bool>
0197 {
0198     return true;
0199 }
0200 
0201 template <class T, std::size_t N>
0202 constexpr bool is_initializable(int) noexcept {
0203     return false;
0204 }
0205 
0206 ///////////////////// Helpers for range size detection
0207 template <std::size_t Begin, std::size_t Last>
0208 using is_one_element_range = std::integral_constant<bool, Begin == Last>;
0209 
0210 using multi_element_range = std::false_type;
0211 using one_element_range = std::true_type;
0212 
0213 ///////////////////// Fields count next expected compiler limitation
0214 constexpr std::size_t fields_count_compiler_limitation_next(std::size_t n) noexcept {
0215 #if defined(_MSC_VER) && (_MSC_VER <= 1920)
0216     if (n < 1024)
0217         return 1024;
0218 #else
0219     static_cast<void>(n);
0220 #endif
0221     return SIZE_MAX;
0222 }
0223 
0224 ///////////////////// Fields count upper bound based on sizeof(T)
0225 template <class T>
0226 constexpr std::size_t fields_count_upper_bound_loose() noexcept {
0227     return sizeof(T) * CHAR_BIT;
0228 }
0229 
0230 ///////////////////// Fields count binary search.
0231 // Template instantiation: depth is O(log(result)), count is O(log(result)), cost is O(result * log(result)).
0232 template <class T, std::size_t Begin, std::size_t Last>
0233 constexpr std::size_t fields_count_binary_search(detail::one_element_range, long) noexcept {
0234     static_assert(
0235         Begin == Last,
0236         "====================> Boost.PFR: Internal logic error."
0237     );
0238     return Begin;
0239 }
0240 
0241 template <class T, std::size_t Begin, std::size_t Last>
0242 constexpr std::size_t fields_count_binary_search(detail::multi_element_range, int) noexcept;
0243 
0244 template <class T, std::size_t Begin, std::size_t Last>
0245 constexpr auto fields_count_binary_search(detail::multi_element_range, long) noexcept
0246     -> detail::enable_if_initializable_helper_t<T, (Begin + Last + 1) / 2>
0247 {
0248     constexpr std::size_t next_v = (Begin + Last + 1) / 2;
0249     return detail::fields_count_binary_search<T, next_v, Last>(detail::is_one_element_range<next_v, Last>{}, 1L);
0250 }
0251 
0252 template <class T, std::size_t Begin, std::size_t Last>
0253 constexpr std::size_t fields_count_binary_search(detail::multi_element_range, int) noexcept {
0254     constexpr std::size_t next_v = (Begin + Last + 1) / 2 - 1;
0255     return detail::fields_count_binary_search<T, Begin, next_v>(detail::is_one_element_range<Begin, next_v>{}, 1L);
0256 }
0257 
0258 template <class T, std::size_t Begin, std::size_t N>
0259 constexpr std::size_t fields_count_upper_bound(int, int) noexcept {
0260     return N - 1;
0261 }
0262 
0263 template <class T, std::size_t Begin, std::size_t N>
0264 constexpr auto fields_count_upper_bound(long, long) noexcept
0265     -> std::enable_if_t<(N > detail::fields_count_upper_bound_loose<T>()), std::size_t>
0266 {
0267     static_assert(
0268         !detail::is_initializable<T, detail::fields_count_upper_bound_loose<T>() + 1>(1L),
0269         "====================> Boost.PFR: Types with user specified constructors (non-aggregate initializable types) are not supported.");
0270     return detail::fields_count_upper_bound_loose<T>();
0271 }
0272 
0273 template <class T, std::size_t Begin, std::size_t N>
0274 constexpr auto fields_count_upper_bound(long, int) noexcept
0275     -> detail::enable_if_initializable_helper_t<T, N>
0276 {
0277     constexpr std::size_t next_optimal = Begin + (N - Begin) * 2;
0278     constexpr std::size_t next = detail::min_of_size_t(next_optimal, detail::fields_count_compiler_limitation_next(N));
0279     return detail::fields_count_upper_bound<T, Begin, next>(1L, 1L);
0280 }
0281 
0282 ///////////////////// Fields count lower bound linear search.
0283 // Template instantiation: depth is O(log(result)), count is O(result), cost is O(result^2).
0284 template <class T, std::size_t Begin, std::size_t Last, class RangeSize, std::size_t Result>
0285 constexpr std::size_t fields_count_lower_bound(RangeSize, size_t_<Result>) noexcept {
0286     return Result;
0287 }
0288 
0289 template <class T, std::size_t Begin, std::size_t Last>
0290 constexpr std::size_t fields_count_lower_bound(detail::one_element_range, size_t_<0> = {}) noexcept {
0291     static_assert(
0292         Begin == Last,
0293         "====================> Boost.PFR: Internal logic error."
0294     );
0295     return detail::is_initializable<T, Begin>(1L) ? Begin : 0;
0296 }
0297 
0298 template <class T, std::size_t Begin, std::size_t Last>
0299 constexpr std::size_t fields_count_lower_bound(detail::multi_element_range, size_t_<0> = {}) noexcept {
0300     // Binary partition to limit template depth.
0301     constexpr std::size_t middle = Begin + (Last - Begin) / 2;
0302     constexpr std::size_t result_maybe = detail::fields_count_lower_bound<T, Begin, middle>(
0303         detail::is_one_element_range<Begin, middle>{}
0304     );
0305     return detail::fields_count_lower_bound<T, middle + 1, Last>(
0306         detail::is_one_element_range<middle + 1, Last>{},
0307         size_t_<result_maybe>{}
0308     );
0309 }
0310 
0311 template <class T, std::size_t Begin, std::size_t Result>
0312 constexpr std::size_t fields_count_lower_bound_unbounded(int, size_t_<Result>) noexcept {
0313     return Result;
0314 }
0315 
0316 template <class T, std::size_t Begin>
0317 constexpr auto fields_count_lower_bound_unbounded(long, size_t_<0>) noexcept
0318     -> std::enable_if_t<(Begin >= detail::fields_count_upper_bound_loose<T>()), std::size_t>
0319 {
0320     static_assert(
0321         detail::is_initializable<T, detail::fields_count_upper_bound_loose<T>()>(1L),
0322         "====================> Boost.PFR: Type must be aggregate initializable.");
0323     return detail::fields_count_upper_bound_loose<T>();
0324 }
0325 
0326 template <class T, std::size_t Begin>
0327 constexpr std::size_t fields_count_lower_bound_unbounded(int, size_t_<0>) noexcept {
0328     constexpr std::size_t last = detail::min_of_size_t(Begin * 2, detail::fields_count_upper_bound_loose<T>()) - 1;
0329     constexpr std::size_t result_maybe = detail::fields_count_lower_bound<T, Begin, last>(
0330         detail::is_one_element_range<Begin, last>{}
0331     );
0332     return detail::fields_count_lower_bound_unbounded<T, last + 1>(1L, size_t_<result_maybe>{});
0333 }
0334 
0335 ///////////////////// Choosing between array size, unbounded binary search, and linear search followed by unbounded binary search.
0336 template <class T>
0337 constexpr auto fields_count_dispatch(long, long, std::false_type /*are_preconditions_met*/) noexcept {
0338     return 0;
0339 }
0340 
0341 template <class T>
0342 constexpr auto fields_count_dispatch(long, long, std::true_type /*are_preconditions_met*/) noexcept
0343     -> std::enable_if_t<std::is_array<T>::value, std::size_t>
0344 {
0345     return sizeof(T) / sizeof(std::remove_all_extents_t<T>);
0346 }
0347 
0348 template <class T>
0349 constexpr auto fields_count_dispatch(long, int, std::true_type /*are_preconditions_met*/) noexcept
0350     -> decltype(sizeof(T{}))
0351 {
0352     constexpr std::size_t typical_fields_count = 4;
0353     constexpr std::size_t last = detail::fields_count_upper_bound<T, typical_fields_count / 2, typical_fields_count>(1L, 1L);
0354     return detail::fields_count_binary_search<T, 0, last>(detail::is_one_element_range<0, last>{}, 1L);
0355 }
0356 
0357 template <class T>
0358 constexpr std::size_t fields_count_dispatch(int, int, std::true_type /*are_preconditions_met*/) noexcept {
0359     // T is not default aggregate initializable. This means that at least one of the members is not default-constructible.
0360     // Use linear search to find the smallest valid initializer, after which we unbounded binary search for the largest.
0361     constexpr std::size_t begin = detail::fields_count_lower_bound_unbounded<T, 1>(1L, size_t_<0>{});
0362 
0363     constexpr std::size_t last = detail::fields_count_upper_bound<T, begin, begin + 1>(1L, 1L);
0364     return detail::fields_count_binary_search<T, begin, last>(detail::is_one_element_range<begin, last>{}, 1L);
0365 }
0366 
0367 ///////////////////// Returns fields count
0368 template <class T>
0369 constexpr std::size_t fields_count() noexcept {
0370     using type = std::remove_cv_t<T>;
0371 
0372     constexpr bool type_is_complete = detail::is_complete<type>::value;
0373     static_assert(
0374         type_is_complete,
0375         "====================> Boost.PFR: Type must be complete."
0376     );
0377 
0378     constexpr bool type_is_not_a_reference = !std::is_reference<type>::value
0379          || !type_is_complete // do not show assert if previous check failed
0380     ;
0381     static_assert(
0382         type_is_not_a_reference,
0383         "====================> Boost.PFR: Attempt to get fields count on a reference. This is not allowed because that could hide an issue and different library users expect different behavior in that case."
0384     );
0385 
0386 #if BOOST_PFR_HAS_GUARANTEED_COPY_ELISION
0387     constexpr bool type_fields_are_move_constructible = true;
0388 #else
0389     constexpr bool type_fields_are_move_constructible =
0390         std::is_copy_constructible<std::remove_all_extents_t<type>>::value || (
0391             std::is_move_constructible<std::remove_all_extents_t<type>>::value
0392             && std::is_move_assignable<std::remove_all_extents_t<type>>::value
0393         )
0394         || !type_is_not_a_reference // do not show assert if previous check failed
0395     ;
0396     static_assert(
0397         type_fields_are_move_constructible,
0398         "====================> Boost.PFR: Type and each field in the type must be copy constructible (or move constructible and move assignable)."
0399     );
0400 #endif  // #if !BOOST_PFR_HAS_GUARANTEED_COPY_ELISION
0401 
0402     constexpr bool type_is_not_polymorphic = !std::is_polymorphic<type>::value;
0403     static_assert(
0404         type_is_not_polymorphic,
0405         "====================> Boost.PFR: Type must have no virtual function, because otherwise it is not aggregate initializable."
0406     );
0407 
0408 #ifdef __cpp_lib_is_aggregate
0409     constexpr bool type_is_aggregate =
0410         std::is_aggregate<type>::value             // Does not return `true` for built-in types.
0411         || std::is_scalar<type>::value;
0412     static_assert(
0413         type_is_aggregate,
0414         "====================> Boost.PFR: Type must be aggregate initializable."
0415     );
0416 #else
0417     constexpr bool type_is_aggregate = true;
0418 #endif
0419 
0420 // Can't use the following. See the non_std_layout.cpp test.
0421 //#if !BOOST_PFR_USE_CPP17
0422 //    static_assert(
0423 //        std::is_standard_layout<type>::value,   // Does not return `true` for structs that have non standard layout members.
0424 //        "Type must be aggregate initializable."
0425 //    );
0426 //#endif
0427 
0428     constexpr bool no_errors =
0429         type_is_complete && type_is_not_a_reference && type_fields_are_move_constructible
0430         && type_is_not_polymorphic && type_is_aggregate;
0431 
0432     constexpr std::size_t result = detail::fields_count_dispatch<type>(1L, 1L, std::integral_constant<bool, no_errors>{});
0433 
0434     detail::assert_first_not_base<type, result>(1L);
0435 
0436 #ifndef __cpp_lib_is_aggregate
0437     constexpr bool type_is_aggregate_initializable_n =
0438         detail::is_aggregate_initializable_n<type, result>::value  // Does not return `true` for built-in types.
0439         || std::is_scalar<type>::value;
0440     static_assert(
0441         type_is_aggregate_initializable_n,
0442         "====================> Boost.PFR: Types with user specified constructors (non-aggregate initializable types) are not supported."
0443     );
0444 #else
0445     constexpr bool type_is_aggregate_initializable_n = true;
0446 #endif
0447 
0448     static_assert(
0449         result != 0 || std::is_empty<type>::value || std::is_fundamental<type>::value || std::is_reference<type>::value || !no_errors || !type_is_aggregate_initializable_n,
0450         "====================> Boost.PFR: If there's no other failed static asserts then something went wrong. Please report this issue to the github along with the structure you're reflecting."
0451     );
0452 
0453     return result;
0454 }
0455 
0456 }}} // namespace boost::pfr::detail
0457 
0458 #ifdef __clang__
0459 #   pragma clang diagnostic pop
0460 #endif
0461 
0462 #endif // BOOST_PFR_DETAIL_FIELDS_COUNT_HPP