Why does MSVC build this without any error or warning? Is something ambiguous in this code? The GCC compiler returns an error because the function f
is private.
#include <stdio.h>
class A {
private:
bool f(void) {return true;};
};
class B : public A {
};
class C : public B {
public:
using A::f;
};
int main() {
C c;
if (c.f()) {
printf("Access to private function\n");
}
return 0;
}
For an example have a look here: https://godbolt.org/z/I5mUSa
It is an MSVC bug. [namespace.udecl]/18:
In a using-declarator that does not name a constructor, all members of the set of introduced declarations shall be accessible. In a using-declarator that names a constructor, no access check is performed. In particular, if a derived class uses a using-declarator to access a member of a base class, the member name shall be accessible. If the name is that of an overloaded member function, then all functions named shall be accessible. The base class members mentioned by a using-declarator shall be visible in the scope of at least one of the direct base classes of the class where the using-declarator is specified.
As A::f
is not accessible in C
, the program is ill-formed (at using A::f
), so the compiler should reject it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With