|
Boost : |
From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2005-01-30 17:12:59
> If memory serves the white space skipping isn't done until something is
> actually going to be pulled off the rdbuf anyway (I could be wrong, of
> course).
I guess you're right about this. It sounds right to me.
> My point is though, if you're going to use operator<< and operator>> with
> istream/ostream in a manner that is not default, then you should
> accomodate for the method you're breaking the defaults yourself in your
> operator<< and operator>> - as opposed to forcing everyone ELSE who uses
> operator<< and operator>> to ensure they get things in the default manner
> (which often the client programmer can't do if they didn't write the
> original code).
It's not a matter of defaults. I agree with what Kevlin said about
extraction operator should not assume the stream is flagged in this way
or another, but make sure it's in the exact form it needs to be, or keep
the flags as they are *if* this is truely a point where the extraction
operator leaves room for caller to manipulate its behaviour. In case of
third-party code, then I agree a customization parameter to lexical_cast
would be best.
>>Unfortunately, it's not that easy. calling lexical_cast<T, noskipws>
>>according to your second proposal, will make it not ignore leading
>>whitespace, but it will still ignore trailing whitespace. Ignoring/not
>>ignoring trailing whitespace requires some additional code other than
>>setting ios_base flags.
>>
>>I'm not saying it's not possible, just that it's not that easy :-(
>
>
> not so.
>
> Right now, trailing white space is skipped only because we actually have
> this code:
>
> T result;
> ss >> result >> std::ws;
> if (!ss.eof())
> throw bad_lexical_cast;
>
> We should just take out the '>> std::ws' as well. Which should then not
> skip trailing white space on noskipws, and if memory serves, will do so if
> skipws is on. Even if it doesn't,
It doesn't, but (scroll down...)
> its a simple matter of:
>
> T result;
> ss >> result;
> if (ss.flags() & skipws)
> ss >> std::ws;
> if (!ss.eof())
> throw bad_lexical_cast;
this should work.
> This handles what we want beautifully. If skipws is on, then white space
> at the beginning and end are skipped, AND white space between values
> (remember the original string "4 5"). If skipws is off, then no white
> space is skipped anywhere, and if you have leading or trailing white
> space, you'd better handle it, or the eof() check will fail.
I think this is a good suggestion (except for the default skipws part :-) )
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk