Boost logo

Boost :

From: Martin Adrian (adrianm_at_[hidden])
Date: 2006-04-15 02:48:32


David Abrahams <dave <at> boost-consulting.com> writes:

> Was there any discussion of the to_string(x) and string_to<T>(x)
> alternative?

I am using an interface which works like this

to_string(T x);
to_string(T x, const locale& l);
// The following function always use the classic locale
// this allows easy specializations for trivial cases like int
to_string_classic(T x);

from_string(SequenceT s);
from_string(SequenceT s, const locale& l);
from_string_classic(SequenceT s);
from_string(SequenceT s, T def); // default value = no throw
from_string(SequenceT s, const locale& l, T def);
// format is applied to stream before value is extracted
// e.g. double x = from_string(str, noskipws);
// int x = from_string(str, hex);
from_string(SequenceT s, FormatT format);
...

On top of this I have also implemented the following:

1. tuple streaming without separators which means I can do:

to_string(make_tuple(hex, setw(10), right, 123));

2. a class that holds the stream to avoid creating it or specifying locale and
formats every time.

string_conversion conv(mylocale, hex);
for (int ii = 0; ii < 10; ++ii)
  str += conv.to_string(ii);


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