Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the MSVC compiler give access to this private function without a warning or error?

Tags:

c++

visual-c++

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

like image 823
John Sinclair Avatar asked Mar 16 '19 08:03

John Sinclair


1 Answers

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.

like image 129
geza Avatar answered Nov 07 '22 16:11

geza