1

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.

  1. If I know the names of the members whose access I care about.
  2. 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?

cigien
  • 57,834
  • 11
  • 73
  • 112
  • You might handle some other special cases, as `aggregate`, but I don't think you can create your traits generically without future reflection. – Jarod42 Mar 26 '20 at 17:20
  • 1
    *"replacing any private with public"* doesn't change overload selected, but then, dispaching with SFINAE might allow to select different function. – Jarod42 Mar 26 '20 at 17:25
  • @Jarod42 That seems like it would work for member functions, but would that work for data members? – cigien Mar 26 '20 at 17:35
  • You meant, something like [that](https://coliru.stacked-crooked.com/a/b77a566823252b5b) – Jarod42 Mar 26 '20 at 17:53
  • Yes, exactly. Except, can I do that if I don't have a name, like i? Also, isn't that code an example of "changing the behaviour of a program by changing the access specifier"? – cigien Mar 26 '20 at 20:19
  • I don't think detection can happen without name. And your quote about changing access is "wrong". it can change behavior, but name selected is the same (even if `C::i` is private, public `C::A::i` has not been chosen for `i` detection). – Jarod42 Mar 26 '20 at 21:13

0 Answers0