From 72723a096d68a47c51b3adfeeeaab203e8424ef9 Mon Sep 17 00:00:00 2001 From: Christian Parpart Date: Wed, 11 Dec 2024 21:54:34 +0100 Subject: [PATCH] Rework CountMembers Signed-off-by: Christian Parpart --- include/reflection-cpp/reflection.hpp | 50 ++++++++------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/include/reflection-cpp/reflection.hpp b/include/reflection-cpp/reflection.hpp index c2b6e61..4e9b415 100644 --- a/include/reflection-cpp/reflection.hpp +++ b/include/reflection-cpp/reflection.hpp @@ -101,32 +101,11 @@ inline constexpr auto JoinStringLiterals = detail::join(); namespace detail { - struct any_t final + // This helper-struct is only used by CountMembers to count the number of members in an aggregate type + struct AnyType final { -#if defined(__clang__) - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Weverything" - template - requires(!std::same_as && !std::same_as) - [[maybe_unused]] constexpr operator T() const; - #pragma clang diagnostic pop -#elif defined(_MSC_VER) - template - requires(!std::same_as && !std::same_as) - [[maybe_unused]] constexpr operator T() const; -#else - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wmissing-declarations" template - requires(!std::same_as && !std::same_as) [[maybe_unused]] constexpr operator T() const; - #pragma GCC diagnostic pop -#endif - - [[maybe_unused]] constexpr operator std::string_view() const - { - return {}; - } }; template @@ -142,21 +121,22 @@ namespace detail // return std::source_location::current().function_name(); return REFLECTION_PRETTY_FUNCTION; } + + template + requires(std::is_aggregate_v) + constexpr inline auto CountMembers = []() constexpr { + if constexpr (requires { AggregateType { Args {}..., AnyType {} }; }) + return CountMembers; + else + return sizeof...(Args); + }(); + } // namespace detail -template +// Count the number of members in an aggregate type. +template requires(std::is_aggregate_v>) -inline constexpr auto CountMembers = [] { - using V = std::remove_cvref_t; - if constexpr (requires { V { Args {}..., detail::any_t {} }; }) - { - return CountMembers; - } - else - { - return sizeof...(Args); - } -}(); +constexpr inline auto CountMembers = detail::CountMembers>; constexpr size_t MaxReflectionMemerCount = 50; // should go as high as 256, I guess? for now 30 is enough