Casting and inheritance, oh my.

Jonathan Wakely jwakely.gcc@gmail.com
Thu Apr 5 01:15:00 GMT 2012


On 5 April 2012 01:16, Sam Varshavchik wrote:
> If I feed the following to gcc 4.6.3:
>
> #include <sstream>
>
> class Y : public std::ostringstream {
>
> public:
>   using std::ostringstream::operator<<;
> };
>
> Y y;
>
> template<typename x> void cast(const x &z)
> {
>  (std::ostringstream &)y << z;
> }
>
> template<typename x> void inherit(const x &z)

Calling this "inherit" is a bit misleading, the ambiguity is nothing
to do with inheritance, it's because of the using declaration, which
re-declares the ostream::operator<< members as Y::operator<<.

If you don't have that using declaration then everything works as you
probably expect it to.

Without the "using" the conversion sequences will be:

ICS1(M): Y& -> ostream&
ICS2(M): const char[10] -> const char* -> const void*

ICS1(N): Y& -> ostream&
ICS2(N): const char[10] -> const char*

Now we have:
ICS1(M) is the same as ICS1(N)
and
ICS2(N) is better than ICS2(M)

So N is the best viable function and the call is not ambiguous.



More information about the Gcc-help mailing list