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

Subject: [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-01-14 20:32:58


#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
 Keywords: |
-------------------------------------------------------+--------------------
 In optional_io.hpp, the insertion operator is written as:

 {{{
 if ( in.good() )
 {
   int d = in.get();
   if ( d == ' ' )
   {
     T x ;
     in >> x;
     v = x ;
   }
   else
     v = optional<T>() ;
 }
 }}}

 However the assumption that there must be a space on the stream means
 something like this fails:

 {{{
 istringstream in ("test");
 optional<string> s;
 assert (s);
 }}}

 It fails because we eat 'd', throw it away, and then return an empty
 optional object because we didn't get the space we were expecting.

 If the intent was to eat up spare whitespace, I would prepose something
 like this:

 {{{
 if (in)
 {
   T x;
   if (in >> std::ws >> x)
     v = x;
   else
     v = optional<T>();
 }
 return in;
 }}}

 However doesn't that interfere with the intent of the skipws flag? For
 example:

 {{{
 istringstream in (" test1 test2");

 optional<string> s;
 in >> s;
 assert (s);

 in.unsetf (ios::skipws);
 in >> s;
 assert (!s);
 }}}

 This doesn't work with "in >> std::skipws >> x" because the second
 assertion will fail.

 Instead you'd need to use "in >> x", which I believe would is the best
 solution.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/2659>
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