This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Casting and inheritance, oh my.
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Sam Varshavchik <mrsam at courier-mta dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Thu, 5 Apr 2012 02:14:48 +0100
- Subject: Re: Casting and inheritance, oh my.
- References: <cone.1333584976.128009.8483.1000@monster.email-scan.com>
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.