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

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



mathieu.malaterre@xxxxxxxxx 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


.



Relevant Pages

  • Re: error C2440: type cast : cannot convert from std::stringstream to void *
    ... But stringstream should inherit this from its parent ios, ... It appears the compiler does not ... class to call operator void*() on. ...
    (microsoft.public.vc.stl)
  • Re: [C++] Help Please! Cant find error, redefines variable
    ... main returns an int and using (void) to signify an empty parameter list ... > stringstream msg1; ... Actually I think that was dummy code, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: std::string vs Cstring
    ... had problems missmatching the fields to the spec because the compiler beats ... > you convert the stringstream to a string using the .strfunction. ... > sprintf format statements, CString is far easier. ...
    (microsoft.public.vc.mfc)
  • Re: STL headers in VC.Net
    ... for stringstream stuff etc. ... It is recommended to change it or just compile with an old compiler. ... either compile with an old compiler or modify the program (a ... >don't want to modify the program because it will be a hard work. ...
    (comp.lang.cpp)
  • stringstream question
    ... istream* is; ... void f ... ioss.str(initStr); ... Is there a easier/better/clearer way to reset a stringstream? ...
    (comp.lang.cpp)