[Dev-C++] ostringstream and insertion operator problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: A D <a_...@ho...> - 2007-11-01 05:58:22
|
Hi! I'm trying to learn c++ and stumbled upon part of a code which I'm havi=
ng
difficulties to understand:
class BadConversion : public std::runtime_error {
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};
=20
inline std::string stringify(double x)
{
std::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}
the function stringify() converts a double number to string And returns the=
string.=20
I'm having problem with the following line:
if (!(o << x)) //<-----------------------------------problem=20
throw BadConversion("stringify(double)");
according to the api ostringstream is a typedef of basic_ostringstream clas=
s. This insertion (<
basic_ostream&=20
basic_ostream::operator<put(*this, *this, this->fill(), __n).failed())
this->setstate(ios_base::badbit);
}
catch(...)
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) !=3D 0)
__throw_exception_again;
}
}
return *this;
}
You can clearly see from the definition that binary operator "<<" outputs =
an=20
object of basic_ostream reference type. My question is can the operator <<=
=20
return a zero in this case? Because by definition this operator clearly ret=
urns a=20
reference object of type basic_ostream&. If it returns zero(which is teste=
d in=20
order for a exception to be thrown in the former code shown above), shouldn=
't=20
it contradict the fact that this operator can only return an object not a n=
umber?=20
Thanks.
_________________________________________________________________
Express yourself with free Messenger emoticons. Get them today!
http://www.freemessengeremoticons.ca/?icid=3DEMENCA122=
|