0

I tried to compile the following code

struct A{
    int a;
};

struct B : A{};

struct C : A{};

struct D: B,C{
    void f(){
        ++B::A::a;
    }
};

int main()
{}

The compilation of this code resulted in ambiguity error in both clang and gcc. I understand the ambiguity error when instead of ++B::A::a writing ++a. But why are we getting this error when accessing a explicitly? Where is this behavior defined in standard.

Karen Baghdasaryan
  • 2,407
  • 6
  • 24
  • Not sure exactly why, but `++B::a` works as expected. – super Sep 24 '21 at 08:58
  • @super, really weird. – Karen Baghdasaryan Sep 24 '21 at 09:03
  • 1
    @super Well, yes that [works](https://godbolt.org/z/aoajaWbaj), because, as noted in the dupe's [answer](https://stackoverflow.com/a/69003745/4944425), `B::A` is a way to name class `A`, which is inherited both via `B` and `C`, resulting in ambiguity. `B::a` resolves the ambiguity by explicitly naming class `B` and its member. – Bob__ Sep 24 '21 at 09:27

0 Answers0