From: Anubhav on
Hi,

I want to know which clause in the C++ standard guides the "Ambiguous
base class conversion" related error that this code throws. Actually,
there are two issues:

1. There are two 'A' subobjects in B3 and hence 'A' is an ambiguous
base class
2. 'A' is both a public and a private indirect base class in 'B3'.

So since (2) is true, why should (1) be the reason for error?

struct A{};

class B1 : A{};
class B2 : public A{};
struct B3 : B1, B2{};

int main(){
B3 b3;
A &r3 = b3;
}

Dabs.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Vaclav Haisman on
Anubhav wrote, On 1.8.2010 16:48:
> Hi,
>
> I want to know which clause in the C++ standard guides the "Ambiguous
> base class conversion" related error that this code throws. Actually,
> there are two issues:
>
> 1. There are two 'A' subobjects in B3 and hence 'A' is an ambiguous
> base class
> 2. 'A' is both a public and a private indirect base class in 'B3'.
>
> So since (2) is true, why should (1) be the reason for error?
>
> struct A{};
>
> class B1 : A{};
> class B2 : public A{};
> struct B3 : B1, B2{};
>
> int main(){
> B3 b3;
> A &r3 = b3;
> }
Because name resolution/lookup happens before access control.
IOW, access control does not influence name lookup.

--
VH

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
Anubhav wrote:
> Hi,
>
> I want to know which clause in the C++ standard guides the "Ambiguous
> base class conversion" related error that this code throws. Actually,
> there are two issues:
>
> 1. There are two 'A' subobjects in B3 and hence 'A' is an ambiguous
> base class
> 2. 'A' is both a public and a private indirect base class in 'B3'.
>
> So since (2) is true, why should (1) be the reason for error?

because access is never used to resolve ambguity. This is by design.

>
> struct A{};
>
> class B1 : A{};
> class B2 : public A{};
> struct B3 : B1, B2{};
>
> int main(){
> B3 b3;
> A &r3 = b3;
> }
>
> Dabs.
>

{ Quoting without added comments, as above, is best avoided. -mod }


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Johannes Schaub (litb) on
Anubhav wrote:

> Hi,
>
> I want to know which clause in the C++ standard guides the "Ambiguous
> base class conversion" related error that this code throws. Actually,
> there are two issues:
>
> 1. There are two 'A' subobjects in B3 and hence 'A' is an ambiguous
> base class
> 2. 'A' is both a public and a private indirect base class in 'B3'.
>
> So since (2) is true, why should (1) be the reason for error?
>
> struct A{};
>
> class B1 : A{};
> class B2 : public A{};
> struct B3 : B1, B2{};
>
> int main(){
> B3 b3;
> A &r3 = b3;
> }
>

Private means that the base-class is inaccessible. But that doesn't mean
that it is in-visible. The base-class is still visible, and verse 4.10/3
(for the pointer conversion) and verse 8.5.3/4 (for reference binding) does
not restrict this to public base-classes. Rather, if such a
conversion/reference-binding is finally necessitated, you have an ambiguity
error.

Using an explicit C-style cast, you can even cast to a private base class,
circumventing accessibility restrictions in a defined way. You can't convert
to an ambiguous base-class in any case, of course.

To make your code access ->B1 -> A and B2 -> A you can convert in two steps.
It's more difficult if you would have "A" as a direct base class too. I'm
not aware of a way to access a non-static data member/member function of the
other (direct) subobject. The Standard contains a note about that too at
verse 10.1/3

"[Note: a class can be an indirect base class more than once and can be a
direct and an indirect base class. There are limited things that can be done
with such a class. The non-static data members and member functions of the
direct base class cannot be referred to in the scope of the derived class.
....]"


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]