1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>False positive with -Wmaybe-uninitialized</title> 5<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../index.html" title="Boost.Optional"> 8<link rel="up" href="../gotchas.html" title="Gotchas"> 9<link rel="prev" href="mixed_relational_comparisons.html" title="Mixed relational comparisons"> 10<link rel="next" href="../exception_safety_guarantees.html" title="Exception Safety Guarantees"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="mixed_relational_comparisons.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../gotchas.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../exception_safety_guarantees.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_optional.tutorial.gotchas.false_positive_with__wmaybe_uninitialized"></a><a class="link" href="false_positive_with__wmaybe_uninitialized.html" title="False positive with -Wmaybe-uninitialized">False 28 positive with -Wmaybe-uninitialized</a> 29</h4></div></div></div> 30<p> 31 Sometimes on GCC compilers below version 5.1 you may get an -Wmaybe-uninitialized 32 warning when copiling with option -02 on a perfectly valid <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span></code> usage. For instance in this 33 program: 34 </p> 35<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">optional</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 36 37<span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">getitem</span><span class="special">();</span> 38 39<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*[])</span> 40<span class="special">{</span> 41 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">a</span> <span class="special">=</span> <span class="identifier">getitem</span><span class="special">();</span> 42 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">b</span><span class="special">;</span> 43 44 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">argc</span> <span class="special">></span> <span class="number">0</span><span class="special">)</span> 45 <span class="identifier">b</span> <span class="special">=</span> <span class="identifier">argc</span><span class="special">;</span> 46 47 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">!=</span> <span class="identifier">b</span><span class="special">)</span> 48 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> 49 50 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 51<span class="special">}</span> 52</pre> 53<p> 54 This is a bug in the compiler. As a workaround (provided in <a href="http://stackoverflow.com/questions/21755206/how-to-get-around-gcc-void-b-4-may-be-used-uninitialized-in-this-funct" target="_top">this 55 Stack Overflow question</a>) use the following way of initializing 56 an optional containing no value: 57 </p> 58<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">b</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">make_optional</span><span class="special">(</span><span class="keyword">false</span><span class="special">,</span> <span class="keyword">int</span><span class="special">());</span> 59</pre> 60<p> 61 This is obviously redundant, but makes the warning disappear. 62 </p> 63</div> 64<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 65<td align="left"></td> 66<td align="right"><div class="copyright-footer">Copyright © 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright © 2014-2018 Andrzej Krzemieński<p> 67 Distributed under the Boost Software License, Version 1.0. (See accompanying 68 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 69 </p> 70</div></td> 71</tr></table> 72<hr> 73<div class="spirit-nav"> 74<a accesskey="p" href="mixed_relational_comparisons.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../gotchas.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../exception_safety_guarantees.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 75</div> 76</body> 77</html> 78