• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>explicit_operator_bool</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="Chapter 1. Boost.Core">
8<link rel="up" href="../index.html" title="Chapter 1. Boost.Core">
9<link rel="prev" href="exchange.html" title="exchange">
10<link rel="next" href="first_scalar.html" title="first_scalar">
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="exchange.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="first_scalar.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h2 class="title" style="clear: both">
27<a name="core.explicit_operator_bool"></a><a class="link" href="explicit_operator_bool.html" title="explicit_operator_bool">explicit_operator_bool</a>
28</h2></div></div></div>
29<div class="toc"><dl class="toc">
30<dt><span class="section"><a href="explicit_operator_bool.html#core.explicit_operator_bool.overview">Overview</a></span></dt>
31<dt><span class="section"><a href="explicit_operator_bool.html#core.explicit_operator_bool.examples">Examples</a></span></dt>
32<dt><span class="section"><a href="explicit_operator_bool.html#core.explicit_operator_bool.history">History</a></span></dt>
33</dl></div>
34<div class="simplesect">
35<div class="titlepage"><div><div><h3 class="title">
36<a name="idm45312828509824"></a>Authors</h3></div></div></div>
37<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
38          Andrey Semashev
39        </li></ul></div>
40</div>
41<div class="section">
42<div class="titlepage"><div><div><h3 class="title">
43<a name="core.explicit_operator_bool.overview"></a><a class="link" href="explicit_operator_bool.html#core.explicit_operator_bool.overview" title="Overview">Overview</a>
44</h3></div></div></div>
45<p>
46        Header <code class="computeroutput"><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">core</span><span class="special">/</span><span class="identifier">explicit_operator_bool</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
47        provides <code class="computeroutput"><span class="identifier">BOOST_EXPLICIT_OPERATOR_BOOL</span><span class="special">()</span></code>, <code class="computeroutput"><span class="identifier">BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL</span><span class="special">()</span></code> compatibility helper macros that expand
48        to an explicit conversion operator to <code class="computeroutput"><span class="keyword">bool</span></code>.
49        For compilers not supporting explicit conversion operators introduced in
50        C++11 the macros expand to a conversion operator that implements the <a href="http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool" target="_top">safe bool
51        idiom</a>. In case if the compiler is not able to handle safe bool idiom
52        well the macros expand to a regular conversion operator to <code class="computeroutput"><span class="keyword">bool</span></code>.
53      </p>
54</div>
55<div class="section">
56<div class="titlepage"><div><div><h3 class="title">
57<a name="core.explicit_operator_bool.examples"></a><a class="link" href="explicit_operator_bool.html#core.explicit_operator_bool.examples" title="Examples">Examples</a>
58</h3></div></div></div>
59<p>
60        Both macros are intended to be placed within a user's class definition. The
61        generated conversion operators will be implemented in terms of <code class="computeroutput"><span class="keyword">operator</span><span class="special">!()</span></code>
62        that should be defined by user in this class. In case of <code class="computeroutput"><span class="identifier">BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL</span><span class="special">()</span></code> the generated conversion operator will
63        be declared <code class="computeroutput"><span class="keyword">constexpr</span></code> which
64        requires the corresponding <code class="computeroutput"><span class="keyword">operator</span><span class="special">!()</span></code> to also be <code class="computeroutput"><span class="keyword">constexpr</span></code>.
65      </p>
66<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span>
67<span class="keyword">class</span> <span class="identifier">my_ptr</span>
68<span class="special">{</span>
69    <span class="identifier">T</span><span class="special">*</span> <span class="identifier">m_p</span><span class="special">;</span>
70
71<span class="keyword">public</span><span class="special">:</span>
72    <span class="identifier">BOOST_EXPLICIT_OPERATOR_BOOL</span><span class="special">()</span>
73
74    <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!()</span> <span class="keyword">const</span>
75    <span class="special">{</span>
76        <span class="keyword">return</span> <span class="special">!</span><span class="identifier">m_p</span><span class="special">;</span>
77    <span class="special">}</span>
78<span class="special">};</span>
79</pre>
80<p>
81        Now <code class="computeroutput"><span class="identifier">my_ptr</span></code> can be used in
82        conditional expressions, similarly to a regular pointer:
83      </p>
84<pre class="programlisting"><span class="identifier">my_ptr</span><span class="special">&lt;</span> <span class="keyword">int</span> <span class="special">&gt;</span> <span class="identifier">p</span><span class="special">;</span>
85<span class="keyword">if</span> <span class="special">(</span><span class="identifier">p</span><span class="special">)</span>
86    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"true"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
87</pre>
88</div>
89<div class="section">
90<div class="titlepage"><div><div><h3 class="title">
91<a name="core.explicit_operator_bool.history"></a><a class="link" href="explicit_operator_bool.html#core.explicit_operator_bool.history" title="History">History</a>
92</h3></div></div></div>
93<h5>
94<a name="core.explicit_operator_bool.history.h0"></a>
95        <span class="phrase"><a name="core.explicit_operator_bool.history.boost_1_56"></a></span><a class="link" href="explicit_operator_bool.html#core.explicit_operator_bool.history.boost_1_56">boost
96        1.56</a>
97      </h5>
98<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
99<li class="listitem">
100            Added new macros <code class="computeroutput"><span class="identifier">BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT</span></code>
101            and <code class="computeroutput"><span class="identifier">BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL</span></code>
102            to define <code class="computeroutput"><span class="keyword">noexcept</span></code> and
103            <code class="computeroutput"><span class="keyword">constexpr</span></code> operators.
104          </li>
105<li class="listitem">
106            The header moved to Boost.Core.
107          </li>
108</ul></div>
109<h5>
110<a name="core.explicit_operator_bool.history.h1"></a>
111        <span class="phrase"><a name="core.explicit_operator_bool.history.boost_1_55"></a></span><a class="link" href="explicit_operator_bool.html#core.explicit_operator_bool.history.boost_1_55">boost
112        1.55</a>
113      </h5>
114<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
115            The macro was extracted from Boost.Log.
116          </li></ul></div>
117</div>
118</div>
119<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
120<td align="left"></td>
121<td align="right"><div class="copyright-footer">Copyright © 2014 Peter Dimov<br>Copyright © 2014 Glen Fernandes<br>Copyright © 2014 Andrey Semashev<p>
122        Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost
123        Software License, Version 1.0</a>.
124      </p>
125</div></td>
126</tr></table>
127<hr>
128<div class="spirit-nav">
129<a accesskey="p" href="exchange.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="first_scalar.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
130</div>
131</body>
132</html>
133