Re: [Boost-bugs] [Boost C++ Libraries] #2659: optional_io insertion operator assumes presence of whitespace on stream

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #2659: optional_io insertion operator assumes presence of whitespace on stream
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2009-02-05 14:40:21


#2659: optional_io insertion operator assumes presence of whitespace on stream
--------------------------------------------------------+-------------------
  Reporter: Andrew Troschinetz <ast_at_[hidden]> | Owner: fcacciola
      Type: Bugs | Status: new
 Milestone: Boost 1.38.0 | Component: optional
   Version: Boost 1.37.0 | Severity: Problem
Resolution: | Keywords:
--------------------------------------------------------+-------------------

Comment(by Andrew Troschinetz <ast_at_[hidden]>):

 If I'm being a nuisance by continuing to reply to this ticket please let
 me know. I'm learning a lot about streams from trying to figure out this
 problem; allow me to refine the description of the problem I'm trying to
 fix and the code I posted in the previous reply.

 I think any given optional type should behave as much like the type it
 wraps as possible. For example:
 {{{
 // This:
 stringstream sin ("1.2.3.4.5");
 optional<double> token;
 while (sin >> token) cout << token << endl;

 // Should have the same output as this:
 stringstream sin ("1.2.3.4.5");
 double token;
 while (sin >> token) cout << token << endl;
 }}}

 Also this should work (as of boost 1.37.0 it does not):
 {{{
 stringstream sin ("test");
 optional<string> token;
 sin >> token; // fails to read "test", instead we get an empty optional
 type
 }}}

 However I think that using an optional type should offer a little more
 than just that (call me crazy). Since it's semantically valid to have an
 un-initialized optional type, I think failure to extract something off the
 stream should return an empty optional type '''without''' setting the
 failbit on the stream. So like:

 {{{
 // outputs 1 nan 5.5 nan nan nan -3.2 nan
 istringstream sin (" 1 this 5.5 is a test -3.2 a");
 optional<double> token;
 const double nan = numeric_limits<double>::quiet_NaN ();
 while (sin >> token) cout << token.get_value_or (nan) << " ";

 // outputs 1
 istringstream sin (" 1 this 5.5 is a test -3.2 a");
 double token;
 while (sin >> token) cout << token << " ";
 }}}

 With that in mind, here's my preposed refinement:

 {{{
 if (in)
 {
     T x;
     if (in >> x)
     {
         v = x;
     }
     else
     {
         if (in.fail ())
         {
             in.clear ();
             std::string dump;
             in >> dump;
         }
         v = optional<T> ();
     }
 }
 else
 {
     v = optional<T> ();
 }
 return in;
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/2659#comment:3>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:59 UTC