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

            Bug ID: 118346
           Summary: Access to member inherited from virtual base class
                    forbidden in GCC with temporary null pointer instance
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wangbopku15 at gmail dot com
  Target Milestone: ---

The following code does not compile in GCC:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

struct A { int i; };
struct C : virtual public A { };

int main (){
    auto res = ((C*)0)->i;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The diagnostic is:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<source>: In function 'int main()':
<source>:5:25: error: invalid access to non-static data member 'A::i' in
virtual base of NULL object
    5 |     auto res = ((C*)0)->i;
      |                         ^

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Actually, I'm not sure whether this should be valid, but the following code
appears to work in GCC:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

struct A { int i; };
struct C : virtual public A { };

int main (){
      auto res = ((C*)0)->A::i;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

https://godbolt.org/z/rY9Yh6jrG

Also note that if the instance here is not a temporary variable, it works:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

struct A { int i; };
struct C : virtual public A { };

int main (){
    auto no_temporary = (C*)0;
    auto res = no_temporary->i;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


https://godbolt.org/z/Mj4ffoahE


MSVC, EDG, and Clang always accept it:

https://godbolt.org/z/aq6ojdPz7

Reply via email to