Re: [Dev-C++] ostringstream and insertion operator problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jonathan W. <jon...@gm...> - 2007-11-01 10:28:39
|
Hi,
A reference to an object is actually a pointer, which is a number (to which
we have attached a special meaning)
Testing this reference tests the pointer with 0, or NULL. I think you will
find that << returns NULL if it fails.
HTH,
Jonathan
On 11/1/07, A D <a_...@ho...> wrote:
>
>
> Hi! I'm trying to learn c++ and stumbled upon part of a code which I'm
> having
> difficulties to understand:
>
>
> class BadConversion : public std::runtime_error {
> public:
> BadConversion(const std::string& s)
> : std::runtime_error(s)
> { }
> };
>
> 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.
> I'm having problem with the following line:
>
>
> if (!(o << x)) //<-----------------------------------problem
> throw BadConversion("stringify(double)");
>
> according to the api ostringstream is a typedef of basic_ostringstream
> class. This insertion (<
> basic_ostream&
> 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) != 0)
> __throw_exception_again;
> }
> }
> return *this;
> }
>
>
> You can clearly see from the definition that binary operator "<<" outputs
> an
> object of basic_ostream reference type. My question is can the operator <<
> return a zero in this case? Because by definition this operator clearly
> returns a
> reference object of type basic_ostream&. If it returns zero(which is
> tested in
> order for a exception to be thrown in the former code shown above),
> shouldn't
> it contradict the fact that this operator can only return an object not a
> number?
> Thanks.
>
>
> _________________________________________________________________
> Express yourself with free Messenger emoticons. Get them today!
> http://www.freemessengeremoticons.ca/?icid=EMENCA122
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
--
<Morpheus> linux, c'est une question de VI ou de MORE
|