[Boost-bugs] [Boost C++ Libraries] #5783: lexical_cast fails for types whose operator<</>> are templated on stream type

Subject: [Boost-bugs] [Boost C++ Libraries] #5783: lexical_cast fails for types whose operator<</>> are templated on stream type
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-08-15 02:07:00


#5783: lexical_cast fails for types whose operator<</>> are templated on stream
type
-------------------------------------+--------------------------------------
 Reporter: gredner@… | Owner: nasonov
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: lexical_cast
  Version: Boost Development Trunk | Severity: Problem
 Keywords: |
-------------------------------------+--------------------------------------
 The following code does not compile:

 {{{
 #include <boost/lexical_cast.hpp>

 #include <iostream>
 #include <string>

 struct Foo
 {
   Foo() : f(2) {}
   int f;
 };

 template <typename OStream>
 OStream& operator<<(OStream& ostr, const Foo& foo)
 {
   ostr << foo.f;
   return ostr;
 }

 int main()
 {
   Foo foo;

   // OK!
   std::cout << foo << std::endl;

   // Does not compile!
   std::cout << boost::lexical_cast<std::string>(foo) << std::endl;
 }
 }}}

 The problem is that inside lexical_cast there is a stream-like type called
 lexical_stream_limited_src which has a bunch of operator<< overloads which
 all return bool, and the rest of the implementation relies on these being
 called, and treats "x << y" expressions as returning bool. However, in
 the example above, such expressions match the template operator<< instead.

 I ran into this because boost::fusion uses operator<<s of this type. So,
 while it is possible to stream a fusion::pair to an ostream, it is not
 possible to lexical_cast it to string.

 One solution would be to get rid of the bool return type for
 lexical_stream_limited_src::operator<<, and instead for these methods to
 save in a boolean member variable whether the call succeeded or failed,
 and for client code of this class to check the member after the call.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5783>
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:50:07 UTC