Even if it's not what would be the best option, since C++17 we can do template-meta-programming to get the information "how many members structure has", as long as the struct is an aggregate, non-polymorphic, and can be initialized using aggregate initialization.
Note, that unless you do some template-magic you shouldn't really use it.
In C++17 it's still pretty problematic, see https://gist.github.com/ChemiaAion/4118a3598f0b120b7f9c5884e9799a8b.
With C++20 you can do it using requires-expression:
struct UniversalType {
template<typename T>
operator T() {}
};
template<typename T>
consteval auto MemberCounter(auto ...Members) {
if constexpr (requires { T{ Members... }; } == false)
return sizeof...(Members) - 1;
else
return MemberCounter<T>(Members..., UniversalType{});
}
You can see this Reddit post to see discussion about it.
It works by trying to instantiate aggregate initialization with more and more parameters, and if it fails it means, that there is one less member than current instantiation: sizeof...(Members) - 1
.
I'm not the author of the provided code.
For anyone interested, here are some more links related to the topic: