Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-11 09:06:09

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