From: Kevlin Henney (kevlin_at_[hidden])
Date: 2003-06-06 05:14:19
In message <3ab501c32b4c$363afc10$8d6c6f50_at_pc>, Terje Slettebø
>>From: "Pavel Vozenilek" <pavel_vozenilek_at_[hidden]>
>> lexical_cast<> constructs and destroys std::stringstream
>> (including dynamic memory allocation/free.)
>> each time a conversion is done.
>> Maybe specialised version of lexical_cast<> can be developed
>> which takes external, existing stringstream instance as
>> template parameter and reuses it.
>> Very rough idea:
>> template<typename Target, std::stringstream& ss, typename Source>
>> Target lex_cast(Source s)
>> // clear ss
>> ss << s;
>> Target t;
>> ss >> t;
>> return t;
>> extern std::stringstream ss;
>> std::stringstream ss;
>> int a = 99;
>> std::string s = lex_cast<std::string, ss>(a);
>This used to be addressed in the "Future Directions" part of the
>lexical_cast documentation, but I see that that section is now removed.
>Maybe it should be put back, at least the relevant parts? Kevlin?
>Note "anything that involves adding extra arguments for a conversion
>operation is not being considered."
And I guess that we could generalise that to "anything that involves
adding extra template parameters" :->
Seriously, adding an extra parameter to lexical_cast would offer at best
a very limited and odd form of parameterisation. From any reasonable
point of view it would seem peculiar that you could parameterise only on
a global variable. It feels more like a workaround than a genuinely
positive feature -- such a stream could not be local to an object or a
thread, for instance.
In the early days there was a single stream used, but the threading and
re-entrancy issues involved made it a non-starter so it was removed.
However, there may be an option in future: I am considering the
possibility of creating a lexical_caster class template that could be
used to create your own "cast function instances". The motivation for
this was more to do with locales, but I suspect that such an object
could hold onto its stream rather than treating it as transient. The
only issue would be to ensure that the stream was reset every time:
s.clear() followed by s.str(), IIRC.
Kevlin Henney phone: +44 117 942 2990
mailto:kevlin_at_[hidden] mobile: +44 7801 073 508
http://www.curbralan.com fax: +44 870 052 2289
Curbralan: Consultancy + Training + Development + Review
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk