nikkoara wrote:
> I am inclined to believe the following is a compiler error. Any
> opinions?
>
> $ g++ --version | head -n 1; cat -n t.cpp; g++ -c t.cpp
> g++ (GCC) 4.6.1
> 1 struct B { };
> 2 struct D : B { } d;
> 3
> 4 struct X { };
> 5 struct Y : X {
> 6 Y (D const&);
> 7 };
> 8
> 9 X const& ref = d;
> t.cpp:9:16: error: invalid initialization of reference of type ‘const
> X&’ from expression of type ‘D’
I wouldn't consider that a fault. The question, looking at the last line,
is why should the compiler consider the side-trip through a 'Y' for
initialisation of 'ref'? There could be a limitless number of classes that
derive from 'X' and the code doesn't explicitly or implicitly specify one.
> This slightly changed version compiles:
>
> $ g++ --version | head -n 1; cat -n t2.cpp; g++ -c t2.cpp
> g++ (GCC) 4.6.1
> 1 struct X { };
> 2 struct Y : X { };
> 3
> 4 struct B { };
> 5 struct D : B {
> 6 operator Y();
> 7 } d;
> 8
> 9 X const& ref = d;
Here, the number of conversions is limited to the bases of 'd' and the
defined conversion operators, so no code external to this could change the
meaning of this code.
Uli
--
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]