Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2002-02-02 08:49:40


Jon -

Jon Wang wrote:
> The expected output of the following code should be "123.1234567".
> ....
> But only can we get "123.123", for the std::stringstream uses the
> default precision. Is this a problem? If so, how can we fix it?

Yes, this is a problem that has been discussed before:
http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?LexicalCast

> #include <iomanip>
> //...
> template<typename Target, typename Source>
> Target lexical_cast(Source arg) {
> //...
> if(!(interpreter << std::setprecision(16) << arg) ||
> !(interpreter >> result) ||
> !(interpreter >> std::ws).eof())
> //...
>
> Maybe the above is an alternative solution. But I wonder if there's
> any portability problem. I've tested it on Borland C++ Compiler 5.5.1
> and Intel C/C++ Compiler 5.0.

This is (as I recall) the way I solved the problem under gcc 2.95.2 for a
project awhile back. A similar solution would be to use template specialization
so that this code only applies to floating point types. As for the portability
of the '16', it might need to be configurable depending on the compiler.

Jeff


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