Boost C++ Libraries: Ticket #10825: Undefined stream insertion operator in boost::optional . https://svn.boost.org/trac10/ticket/10825 <p> I get undefined reference error while using boost::optional. Following code can be used to reproduce the error: </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/optional.hpp&gt;</span><span class="cp"></span> <span class="kt">void</span> <span class="nf">print_optional</span><span class="p">(</span><span class="kt">int</span> <span class="n">type</span><span class="p">)</span> <span class="p">{</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">value</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">type</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="n">value</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span> <span class="p">}</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">value</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">print_optional</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="n">test_opt</span><span class="p">.</span><span class="nl">cpp</span><span class="p">:(.</span><span class="n">text</span><span class="o">+</span><span class="mh">0x64</span><span class="p">)</span><span class="o">:</span> <span class="n">undefined</span> <span class="n">reference</span> <span class="n">to</span> <span class="err">`</span><span class="n">std</span><span class="o">::</span><span class="n">basic_ostream</span><span class="o">&lt;</span><span class="kt">char</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">char_traits</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;&amp;</span> <span class="n">boost</span><span class="o">::</span><span class="k">operator</span><span class="o">&lt;&lt;</span> <span class="o">&lt;</span><span class="kt">char</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">char_traits</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span><span class="p">,</span> <span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">basic_ostream</span><span class="o">&lt;</span><span class="kt">char</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">char_traits</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;&amp;</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="k">const</span><span class="o">&amp;</span><span class="p">)</span><span class="err">&#39;</span> <span class="nl">collect2</span><span class="p">:</span> <span class="nl">error</span><span class="p">:</span> <span class="n">ld</span> <span class="n">returned</span> <span class="mi">1</span> <span class="n">exit</span> <span class="n">status</span> </pre></div></div><p> We are getting this because starting boost-1.56, stream insertion operator is declared in <strong>optional.hpp</strong> but not defined: </p> <div class="wiki-code"><div class="code"><pre><span class="c1">// Forward declaration to prevent operator safe-bool from being used.</span> <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">CharType</span><span class="p">,</span> <span class="k">class</span> <span class="nc">CharTrait</span><span class="p">,</span> <span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span> <span class="n">std</span><span class="o">::</span><span class="n">basic_ostream</span><span class="o">&lt;</span><span class="n">CharType</span><span class="p">,</span> <span class="n">CharTrait</span><span class="o">&gt;&amp;</span> <span class="k">operator</span><span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">basic_ostream</span><span class="o">&lt;</span><span class="n">CharType</span><span class="p">,</span> <span class="n">CharTrait</span><span class="o">&gt;&amp;</span> <span class="n">out</span><span class="p">,</span> <span class="n">optional</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="k">const</span><span class="o">&amp;</span> <span class="n">v</span><span class="p">);</span> <span class="c1">// There is no definition of this function.</span> </pre></div></div><p> <strong>Possible Solution</strong> </p> <p> Adding following code fixed this issue for me: </p> <div class="wiki-code"><div class="code"><pre><span class="k">namespace</span> <span class="n">boost</span> <span class="p">{</span> <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">CharType</span><span class="p">,</span> <span class="k">class</span> <span class="nc">CharTrait</span><span class="p">,</span> <span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span> <span class="n">std</span><span class="o">::</span><span class="n">basic_ostream</span><span class="o">&lt;</span><span class="n">CharType</span><span class="p">,</span> <span class="n">CharTrait</span><span class="o">&gt;&amp;</span> <span class="k">operator</span><span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">basic_ostream</span><span class="o">&lt;</span><span class="n">CharType</span><span class="p">,</span> <span class="n">CharTrait</span><span class="o">&gt;&amp;</span> <span class="n">out</span><span class="p">,</span> <span class="n">optional</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="k">const</span><span class="o">&amp;</span> <span class="n">v</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="p">{</span> <span class="n">out</span> <span class="o">&lt;&lt;</span> <span class="o">*</span><span class="n">v</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">out</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> </pre></div></div> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10825 Trac 1.4.3 rion4ik@… Mon, 05 Jan 2015 22:16:30 GMT <link>https://svn.boost.org/trac10/ticket/10825#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10825#comment:1</guid> <description> <p> related bug <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/2103"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/2103</a> </p> <p> rolling back changes from <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2103" title="#2103: Bugs: ostream::operator&lt;&lt;(bool) is called if optional_io.hpp is not included (closed: fixed)">#2103</a> fixes this one. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 06 Jan 2015 21:28:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10825#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10825#comment:2</guid> <description> <p> Why you don't include </p> <pre class="wiki"> #include &lt;boost/optional/optional_io.hpp&gt; </pre><p> if you want to print an optional? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>akrzemi1</dc:creator> <pubDate>Tue, 20 Jan 2015 23:13:25 GMT</pubDate> <title>owner, status, milestone changed https://svn.boost.org/trac10/ticket/10825#comment:3 https://svn.boost.org/trac10/ticket/10825#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Fernando Cacciola</span> to <span class="trac-author">akrzemi1</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.58.0</span> </li> </ul> Ticket akrzemi1 Tue, 20 Jan 2015 23:16:48 GMT <link>https://svn.boost.org/trac10/ticket/10825#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10825#comment:4</guid> <description> <p> In order to stream out optional objects you have to include the IO header: </p> <pre class="wiki">#include &lt;boost/optional/optional_io.hpp&gt; </pre><p> I changed the implementation a bit, so that rather than a link-time error you get a compile time error about missing header. The change is now in develop branch. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>akrzemi1</dc:creator> <pubDate>Wed, 18 Feb 2015 14:10:44 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/10825#comment:5 https://svn.boost.org/trac10/ticket/10825#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket