Bug 77725 - An example from the standard regarding member lookup fails to compile
Summary: An example from the standard regarding member lookup fails to compile
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 92890 (view as bug list)
Depends on:
Blocks: c++-lookup, c++-name-lookup
  Show dependency treegraph
 
Reported: 2016-09-24 16:55 UTC by Ilya
Modified: 2022-04-28 22:35 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-04-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ilya 2016-09-24 16:55:11 UTC
Example of valid code from the standard (10.2.7 Member name lookup [class.member.lookup])

compiled with command: "g++ -std=c++11 main.cpp && ./a.out" on Linux

compiler version:
g++ (GCC) 6.1.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
===================================================================================
struct A { int x; };                    // S(x,A) = { { A::x }, { A } }
struct B { float x; };                  // S(x,B) = { { B::x }, { B } }
struct C: public A, public B { };       // S(x,C) = { invalid, { A in C, B in C } }
struct D: public virtual C { };         // S(x,D) = S(x,C)
struct E: public virtual C { char x; }; // S(x,E) = { { E::x }, { E } }
struct F: public D, public E { };       // S(x,F) = S(x,E)

int main() 
{
   F f;
   f.x = 0; // OK, lookup finds E::x
}
===================================================================================
Error message:

main.cpp: In function 'int main()':
main.cpp:11:6: error: request for member 'x' is ambiguous
    f.x = 0; // OK, lookup finds E::x
      ^
main.cpp:2:18: note: candidates are: float B::x
 struct B { float x; };                  // S(x,B) = { { B::x }, { B } }
                  ^
main.cpp:1:16: note:                 int A::x
 struct A { int x; };                    // S(x,A) = { { A::x }, { A } }
                ^
Comment 1 Tamir Aviv 2016-09-25 08:52:57 UTC
It's important to note that if you switch the order of inheritance in struct F 
(
instead of:  "struct F: public D, public E { };"
use:         "struct F: public E, public F { };" 
)

this example compiled without an error, even though the standard say that the order of derivation is not significant (10.1.2 Multiple base classes [class.mi])
Comment 2 Jonathan Wakely 2019-12-10 16:38:27 UTC
*** Bug 92890 has been marked as a duplicate of this bug. ***