[Boost-bugs] [Boost C++ Libraries] #10825: Undefined stream insertion operator in boost::optional .

Subject: [Boost-bugs] [Boost C++ Libraries] #10825: Undefined stream insertion operator in boost::optional .
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-11-28 16:29:17


#10825: Undefined stream insertion operator in boost::optional .
-------------------------------------------------+-----------------------
 Reporter: amit.abhash@… | Owner: fcacciola
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: optional
  Version: Boost 1.56.0 | Severity: Problem
 Keywords: undefined stream insertion operator |
-------------------------------------------------+-----------------------
 I get undefined reference error while using boost::optional. Following
 code can be used to reproduce the error:

 {{{#!c++
 #include <iostream>
 #include <boost/optional.hpp>

 void print_optional(int type)
 {
     boost::optional<int> value;
     if (type == 1) {
         value = boost::optional<int>(1000);
     }

     std::cout << value;
 }

 int main()
 {
     print_optional(1);
 }

 test_opt.cpp:(.text+0x64): undefined reference to
 `std::basic_ostream<char, std::char_traits<char> >& boost::operator<<
 <char, std::char_traits<char>, int>(std::basic_ostream<char,
 std::char_traits<char> >&, boost::optional<int> const&)'
 collect2: error: ld returned 1 exit status
 }}}

 We are getting this because starting boost-1.56, stream insertion operator
 is declared in '''optional.hpp''' but not defined:

 {{{#!c++
 // Forward declaration to prevent operator safe-bool from being used.
 template<class CharType, class CharTrait, class T>
 std::basic_ostream<CharType, CharTrait>&
 operator<<(std::basic_ostream<CharType, CharTrait>& out, optional<T>
 const& v);
 // There is no definition of this function.
 }}}


 '''Possible Solution'''

 Adding following code fixed this issue for me:

 {{{#!c++
 namespace boost {
     template<class CharType, class CharTrait, class T>
     std::basic_ostream<CharType, CharTrait>&
 operator<<(std::basic_ostream<CharType, CharTrait>& out,
                                                         optional<T> const&
 v)
     {
         if (v) {
             out << *v;
         }

         return out;
     }
 }
 }}}

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