Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

error C2440: 'type cast' : cannot convert from 'std::stringstream' to 'void *'

44 views
Skip to first unread message

mathieu....@gmail.com

unread,
Aug 18, 2006, 5:00:56 PM8/18/06
to
Hello,

I don't understand why I cannot do the following with vs7:

-----------------------------------
#include <iostream>
#include <sstream>

int main ()
{
std::stringstream ss;
if ( (void*)ss == 0) {
std::cerr << "zero\n";
}
return 0;
}
-----------------------------------

The compiler is returning:

ss.cxx(7) : error C2440: 'type cast' : cannot convert from
'std::stringstream' to 'void *'
Ambiguous user-defined-conversion

But stringstream should inherit this from its parent ios, right ?

http://cplusplus.com/ref/iostream/ios/operatorvoidpt.html

Thanks
Mathieu

Igor Tandetnik

unread,
Aug 18, 2006, 5:37:00 PM8/18/06
to
mathieu....@gmail.com wrote:
> I don't understand why I cannot do the following with vs7:
>
> -----------------------------------
> std::stringstream ss;
> if ( (void*)ss == 0) {
> std::cerr << "zero\n";
> }
>
> The compiler is returning:
>
> ss.cxx(7) : error C2440: 'type cast' : cannot convert from
> 'std::stringstream' to 'void *'
> Ambiguous user-defined-conversion
>
> But stringstream should inherit this from its parent ios, right ?

Looks like a bug in how virtual base class is handled. It works with
istringstream and ostringstream, but not for stringstream. The
difference is stringstream is derived from ios twice, via istream and
ostream, as a virtual base class. It appears the compiler does not
realize that and fails because it doesn't know which copy of the base
class to call operator void*() on.

The bug appears to affect only implicitly invoked conversion operators.
The following work:

if (!ss) {...}

if (ss.operator void*() == 0) {...}

I don't have VC8 handy at the moment to check there.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


mathieu....@gmail.com

unread,
Aug 18, 2006, 6:52:00 PM8/18/06
to

Igor Tandetnik wrote:
> mathieu....@gmail.com wrote:
> > I don't understand why I cannot do the following with vs7:
> >
> > -----------------------------------
> > std::stringstream ss;
> > if ( (void*)ss == 0) {
> > std::cerr << "zero\n";
> > }
> >
> > The compiler is returning:
> >
> > ss.cxx(7) : error C2440: 'type cast' : cannot convert from
> > 'std::stringstream' to 'void *'
> > Ambiguous user-defined-conversion
> >
> > But stringstream should inherit this from its parent ios, right ?
>
> Looks like a bug in how virtual base class is handled. It works with
> istringstream and ostringstream, but not for stringstream. The
> difference is stringstream is derived from ios twice, via istream and
> ostream, as a virtual base class. It appears the compiler does not
> realize that and fails because it doesn't know which copy of the base
> class to call operator void*() on.
>
> The bug appears to affect only implicitly invoked conversion operators.
> The following work:
>
> if (!ss) {...}
>
> if (ss.operator void*() == 0) {...}
>
> I don't have VC8 handy at the moment to check there.

Thanks Igor, both workarounds work for me.

Mathieu

0 new messages