I'm trying to write code that does this. Something like the following
struct A { int i; };
struct B { private : int i; };
static_assert(has_public_non_static_data_members<A>{});
static_assert(! has_public_non_static_data_members<B>{});
But I can't seem to get this to work. Note that I'm not asking for a way to find out how many public data members there are, or their types, or their names.
I can get similar things to work in some cases.
- If I know the names of the members whose access I care about.
- If I want to check that there are no data members at all (private/public). (ignoring virtual inheritance)
But I can't seem to distinguish between classes that differ only in the access specifiers of members. I assumed that there was probably some way to do this, until I found this on cppreference,
Member access check is the last step after any given language construct is interpreted. The intent of this rule is that replacing any private with public never alters the behavior of the program.
(I couldn't find that exact wording here, but I assume that cppreference is interpreting the text correctly.)
This suggests to me that a solution to my problem would violate this intent. So, does that mean it's not possible at all? Or is there still hope, and I've just missed something?