Boost logo

Boost :

From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2006-09-02 16:00:48


Gregory Dai wrote:
[snip]
>> As usual, the latest documentation is here:
>>
>> http://www.revergestudios.com/boost-exception/boost-exception.htm
>>
>> Here is a zip file with the source code, complete with jamfiles and
>> tests:
>>
>> http://www.revergestudios.com/boost-exception/boost-exception.zip
[snip]
>> The current implementation
>> generates a string representation of each exception info value (at
>> the time
>> exception_info is called) as follows:
>>
>> - If a suitable overload for the unqualified call to to_string
>> (passing the
>> exception info value as the only argument) is found, that overload is
>> used.
>>
>> - Otherwise, if a suitable overload for serializing the exception
>> info value
>> into a std::ostringstream is found, that overload is used.
>>
>> - Otherwise the system gives up and uses a generic "stub" string to
>> indicate
>> that the exception info value could not be converted to string.
>>
>> This is accomplished through the to_string function template, which
>> has the
>> following signature:
>>
>> template <class T>
>> std::string to_string( T const &, char const * stub );
>>
>> Do you think that to_string is universally useful? Is there anything
>> in Boost with similar functionality? Perhaps we can make to_string a
>> separate addition to Boost?
>
>
> std::string s = boost::lexical_cast<std::string>(T const&);

This will not work because it requires that a conversion from T to
std::string is available.

The main use of the exception library is for storing values in exception
objects, both at the time of the throw, and after a catch and before a
re-throw. Purely for logging/debugging purposes, the exception lib can
automatically generate a string that represents all values stored in an
exception object, but it isn't reasonable to require that a conversion to
string is defined for each type users can store in exception objects.

The way the exception lib defines to_string, it will do what
boost::lexical_cast does if such conversion is available; if the type can
not be converted to string, this does not result in compile error, and a
"stub" string representation is used instead (the definition of to_string is
in to_string.hpp in the linked .ZIP file above.)

So let me change my original question about to_string being useful by
itself: isn't it a good idea to include in Boost something like to_string
(or some form of lexical_cast<std::string>) that does not result in a
compile error in the case when the conversion is not possible?

--Emil


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk