简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM