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
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
Thanks Igor, both workarounds work for me.
Mathieu