Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-06-21 21:09:15

(Because this reply is so late, I cc it, as well)

Sorry for the late reply.

> I #included <gregorian\greg_date.hpp> and <gregorian\parsers.hpp> in my
> code, and they indirectly #include <lexical_cast.hpp>. Here is the warning
> get from the compiler (I am using MS Visual C++ 6):
> *****************
> d:\documents and
> : warning C4800: 'void *' : forcing value to bool 'true' or 'false'
> (performance warning)
> d:\documents and
> : while compiling class-template member function 'bool __thiscall
> boost::detail::lexical_stream<int,class std::basic_string<char,struct
> std::char_tr
> aits<char>,class std::allocator<char> > >::operator <<(const class
> std::basic_string<char,struct std::char_traits<char>,class
> std::allocator<char> > &)'
> and others like this
> *****************
> And here is the line that causes this warning. First off, it's not clear
> the return should be a bool, and if that was indeed the author's
> the conversion between the ( stream << input ) which normally should
> a stream&, and bool, does not seem to make sense (for one thing the return
> will never be false).
> ******************
> bool operator<<(const Source &input)
> {
> return stream << input;
> }
> ******************
> Could somebody clarify this.

First, the above warning has been fixed in the latest CVS version (this
issue was brought up a while ago, too). The function is now defined as:

bool operator<<(const Source &input)
    return !(stream << input).fail();

This removes the warning, and does the same as the implicit conversion to
bool did.

The reason the implicit conversion to bool works is that std::stringstream
has an implicit conversion to void *, to give the stream's state. It returns
zero for fail(), and non-zero otherwise, so converted to bool, it becomes
the same as !fail(), as shown above.

Conversion to void *, rather than the apparently more obvious bool is to
avoid the possibility of it being erroneously used in arithmetic expressions
that way, such as:

int value=cin + cout;

There's also a "safe bool" technique, which avoids the possibility of the
conversion to void * being used for something else, like "delete cout;".
That technique is used in Boost ("safe_bool").



Boost list run by bdawes at, gregod at, cpdaniel at, john at