<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.15"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>libzypp: Code Pitfalls - Frequently made mistakes</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="navtree.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="resize.js"></script> <script type="text/javascript" src="navtreedata.js"></script> <script type="text/javascript" src="navtree.js"></script> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(document).ready(initResizable); /* @license-end */</script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td id="projectalign" style="padding-left: 0.5em;"> <div id="projectname">libzypp  <span id="projectnumber">17.9.0</span> </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.15 --> <script type="text/javascript" src="menudata.js"></script> <script type="text/javascript" src="menu.js"></script> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(function() { initMenu('',false,false,'search.php','Search'); }); /* @license-end */</script> <div id="main-nav"></div> </div><!-- top --> <div id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> <div id="nav-sync" class="sync"></div> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(document).ready(function(){initNavTree('CodePitfalls.html','');}); /* @license-end */ </script> <div id="doc-content"> <div class="PageDoc"><div class="header"> <div class="headertitle"> <div class="title">Code Pitfalls - Frequently made mistakes </div> </div> </div><!--header--> <div class="contents"> <div class="textblock"><h1><a class="anchor" id="TriBoolCompare"></a> Comparing TriBool values</h1> <p>Comparing two <a class="el" href="group__BOOST.html#gad228c7343f60318e4c045a2ae77e9c39">TriBool</a> values is not as easy as it might look like, because the <code>TriBool::operator==</code> and <code>TriBool::operator!=</code> return a <a class="el" href="group__BOOST.html#gad228c7343f60318e4c045a2ae77e9c39">TriBool</a>.</p> <p>For example is <code>(indeterminate==indeterminate)</code> not <code>true</code>, but <code>indeterminate</code>. That's why the following snippet does not do what the author expected:</p> <div class="fragment"><div class="line"><span class="comment">// buggy option class</span></div><div class="line"><span class="keyword">struct </span>Option</div><div class="line">{</div><div class="line"> <span class="keyword">public</span>:</div><div class="line"> <a class="code" href="structzypp_1_1Option.html#a9881a7092f9c2b87ac091f037b90c1dc">Option</a>()</div><div class="line"> : <a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a>( indeterminate )</div><div class="line"> {}</div><div class="line"></div><div class="line"> <span class="keywordtype">bool</span> <a class="code" href="structzypp_1_1Option.html#acc6b1bf4aad236f1243bfbb254dc50d0">get</a>()<span class="keyword"> const</span></div><div class="line"><span class="keyword"> </span>{ <span class="keywordflow">return</span> ( <a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a> == indeterminate ) ? true : <a class="code" href="classbool.html">bool</a>(<a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a>); }</div><div class="line"></div><div class="line"> <span class="keywordtype">void</span> <a class="code" href="structzypp_1_1Option.html#ae94eeeb75dd35b40682de97a6bc5bf27">set</a>( <span class="keywordtype">bool</span> new_r )</div><div class="line"> { <a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a> = new_r; }</div><div class="line"></div><div class="line"> <span class="keyword">private</span>:</div><div class="line"> tribool <a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a>;</div><div class="line">};</div></div><!-- fragment --><p>You find that <code>get()</code> returns <code>false</code> as long as the option is unset, and not <code>true</code> as the code suggests. That's because <code>(_value==indeterminate)</code> returns <code>indeterminate</code>.</p> <dl class="section note"><dt>Note</dt><dd>Always use <code>indeterminate(_value)</code> to test whether a TriBools value is <code>indeterminate:</code> </dd></dl> <div class="fragment"><div class="line"><span class="keywordtype">bool</span> get()<span class="keyword"> const</span></div><div class="line"><span class="keyword"></span>{ <span class="keywordflow">return</span> indeterminate( <a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a> ) ? true : <a class="code" href="classbool.html">bool</a>(<a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a>); }</div></div><!-- fragment --><div class="fragment"><div class="line">tribool <a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a>;</div><div class="line">...</div><div class="line">if ( indeterminate( <a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a> ) )</div><div class="line">{ ... } <span class="comment">// indeterminate</span></div><div class="line"><span class="keywordflow">else</span> <span class="keywordflow">if</span> ( <a class="code" href="SysContent_8cc.html#a5566eab00fc081a74f536939218937ad">_value</a> )</div><div class="line">{ ... } <span class="comment">// true</span></div><div class="line"><span class="keywordflow">else</span></div><div class="line">{ ... } <span class="comment">// false</span></div></div><!-- fragment --> <hr/> </div></div><!-- PageDoc --> </div><!-- contents --> </div><!-- doc-content --> <!-- start footer part --> <div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> <ul> <li class="footer">Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.15 </li> </ul> </div> </body> </html>