Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2006-04-11 04:18:54

David Abrahams wrote:
> "Beman Dawes" <bdawes_at_[hidden]> writes:
> > * Boost Lexical Cast has been tentatively accepted by the LWG for TR2.
> Was there any discussion of the to_string(x) and string_to<T>(x)
> alternative?

That would be better than the current lexical_cast, provided that there are also basic_string and wstring vresions, i.e.:

   to_basic_string< CharT, CharTraits, Allocator >( x )

The string_to<T> function could have basic_string and wstring overloads, but this would be inconsistent with the rest of the C++ library.

As has been mentioned, the to_string methods will not fail conversion. However, it is unclear what to do with string_to that may fail. There are two possible interfaces:
1. throw an exception on failure, like is currently done with lexical_cast
2. provide a boolean or enumeration for failure type (not-EOS, read-fail, etc.)

For the second interface, it has been suggested to use something like:

   bool string_to<T>(x, ret)

where ret is an output reference. However, this doesn't allow for the simple one-line interface that lexical_cast provides. Drawing influence from Python, I suggest that the non-throwing version look like:

   std::pair<T,bool> string_to<T>(x)


   fn( string_to<Object>(object).first );

> I only ask because I remember the issue coming up somewhere before,
> and I have never seen a use for lexical_cast that didn't have a string
> on one end.

Yes. Java, Python and other languages have conversion to strings built in to their framework so classes can provide their own mechanism for converting to a string. In C++, the generic case is 3 lines:

   std::ostringstream ss;
   ss << object;
   fn( ss.str());

compared to the single line above.

- Reece
Express yourself instantly with MSN Messenger! Download today it's FREE!

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