Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2005-02-04 15:23:28


From: =?iso-8859-1?Q?Terje_Sletteb=F8?= <tslettebo_at_[hidden]>
> >From: "Preston A. Elder" <prez_at_[hidden]>
> > On Sun, 30 Jan 2005 13:45:21 +0100, Kevlin Henney wrote:
> >
> > > Yes, lexical_cast unsets the skipping of whitespace, but that does not,
> > > to my knowledge, introduce problems for correctly written stream
> > > extraction operators.
>
> > Is it so hard to have lexical_cast have an OPTIONAL second template
> > argument of the std::ios::fmtflags to use (defaulting to the default
> > fmtflags, which is skipws) - and only do the ">> std::ws" which we do if
> > skipws is turned on in the fmtflags we are to use?

I agree with Kevlin that a correctly written extraction operator
should be managing skipws itself. I also agree with Preston that
it is the rare one that is correctly written (wrt skipws, at
least). Thus, it does seem pedantic to cause grief to so many
for something apparently handled quite easily.

> When this has been discussed before, other requests for conversion
> customisation has also come up, such as being able to use a different
> locale, or other stream customisations not available from just the format
> flags (such as setting the number base, or other number formatting).
> Therefore, if something were to be optionally passable, maybe it should be a
> full stream object? (which reminded me of this one:
> http://www.refactoring.com/catalog/preserveWholeObject.html :) )

That's awfully heavyweight for what is rightly a simple, common
expectation, don't you think? I'm sure there are times when that
would be useful, but it really deviates from the concept of a
cast, don't you think?

> However, passing a stream may be a little hard to implement, as you can't
> bind a default argument to a non-const reference, so the following won't
> work:
>
> template<typename Target, typename Source>
> Target lexical_cast(const Source &arg,
> std::basic_stringstream<typename detail::select_stream_char<Source,
> Target>::type> &stream =
> std::basic_stringstream<typename detail::select_stream_char<Source,
> Target>::type>())
> ...
>
> ("select_stream_char<>" is made up for the occasion; it contains the typedef
> for char_type in lexical_stream)

There's no reason you can't overload it, is there?

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

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