#ifndef RFL_INTERNAL_NUM_FIELDS_HPP_ #define RFL_INTERNAL_NUM_FIELDS_HPP_ #include #include #include namespace rfl { namespace internal { struct any { any(std::size_t); template constexpr operator T() const noexcept; }; template struct count_fields_helper { template static consteval bool constructible() { return [](std::index_sequence) { return requires { T{any(is)...}; }; }(std::make_index_sequence()); } template static consteval bool constructible_with_nested() { return []( std::index_sequence, std::index_sequence, std::index_sequence) { return requires { T{any(i)..., {any(j)...}, any(k)...}; }; }(std::make_index_sequence(), std::make_index_sequence(), std::make_index_sequence()); } template static consteval std::size_t count_max_fields() { static_assert(n <= static_cast(sizeof(T))); if constexpr (constructible() && !constructible()) { return n; } else { return count_max_fields(); } } template static consteval std::size_t get_nested_array_size() { if constexpr (size < 1) { return 1; } else if constexpr (constructible_with_nested() && !constructible_with_nested()) { return size; } else { return get_nested_array_size(); } } template static consteval std::size_t count_fields_impl() { static_assert(index <= max); if constexpr (index == max) { return 0; } else { return 1 + count_fields_impl< index + get_nested_array_size(), max>(); } } static consteval std::size_t count_fields() { return count_fields_impl<0, count_max_fields()>(); } }; template constexpr std::size_t num_fields = count_fields_helper::count_fields(); } // namespace internal } // namespace rfl #endif