[Bug c++/95407] New: G++ allows access to base class members from a friend of a derived class

dragondreamer at live dot com gcc-bugzilla@gcc.gnu.org
Fri May 29 09:12:28 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95407

            Bug ID: 95407
           Summary: G++ allows access to base class members from a friend
                    of a derived class
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dragondreamer at live dot com
  Target Milestone: ---

G++ (at least trunk, 10.1.0, 9.3, 9.2) successfully compiles the following
code:

// ===================================
#include <iostream>

struct test_base
{
protected:
    static constexpr int value = 123;
};

template<typename Base>
struct ignored_friend_declaration
{
    static void bug()
    {
        std::cout << Base::value;
    }
};

struct test_child : test_base
{
    using ignored_friend_declaration_t = ignored_friend_declaration<test_base>;
    friend ignored_friend_declaration_t;

    static void test()
    {
        ignored_friend_declaration_t::bug();
    }
};

int main()
{
    test_child::test();
}
// ===================================

According to http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1873,
access to Base::value should be disallowed. Clang produces the following error
message: <source>:14:28: error: 'value' is a protected member of 'test_base'.

See also https://bugs.llvm.org/show_bug.cgi?id=46036


More information about the Gcc-bugs mailing list