summaryrefslogtreecommitdiff
path: root/libs/core/doc/html/core/explicit_operator_bool.html
blob: 983e938a16dd2fe59cf8630780eae9344db6274e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>explicit_operator_bool</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.Core">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.Core">
<link rel="prev" href="enable_if.html" title="enable_if">
<link rel="next" href="ignore_unused.html" title="ignore_unused">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="enable_if.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="ignore_unused.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="core.explicit_operator_bool"></a><a class="link" href="explicit_operator_bool.html" title="explicit_operator_bool">explicit_operator_bool</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="explicit_operator_bool.html#core.explicit_operator_bool.overview">Overview</a></span></dt>
<dt><span class="section"><a href="explicit_operator_bool.html#core.explicit_operator_bool.examples">Examples</a></span></dt>
<dt><span class="section"><a href="explicit_operator_bool.html#core.explicit_operator_bool.history">History</a></span></dt>
</dl></div>
<div class="simplesect">
<div class="titlepage"><div><div><h3 class="title">
<a name="idp93227056"></a>Authors</h3></div></div></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
          Andrey Semashev
        </li></ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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>
</h3></div></div></div>
<p>
        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>
        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
        to an explicit conversion operator to <code class="computeroutput"><span class="keyword">bool</span></code>.
        For compilers not supporting explicit conversion operators introduced in
        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
        idiom</a>. In case if the compiler is not able to handle safe bool idiom
        well the macros expand to a regular conversion operator to <code class="computeroutput"><span class="keyword">bool</span></code>.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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>
</h3></div></div></div>
<p>
        Both macros are intended to be placed within a user's class definition. The
        generated conversion operators will be implemented in terms of <code class="computeroutput"><span class="keyword">operator</span><span class="special">!()</span></code>
        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
        be declared <code class="computeroutput"><span class="keyword">constexpr</span></code> which
        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>.
      </p>
<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>
<span class="keyword">class</span> <span class="identifier">my_ptr</span>
<span class="special">{</span>
    <span class="identifier">T</span><span class="special">*</span> <span class="identifier">m_p</span><span class="special">;</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="identifier">BOOST_EXPLICIT_OPERATOR_BOOL</span><span class="special">()</span>

    <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!()</span> <span class="keyword">const</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="special">!</span><span class="identifier">m_p</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">};</span>
</pre>
<p>
        Now <code class="computeroutput"><span class="identifier">my_ptr</span></code> can be used in
        conditional expressions, similarly to a regular pointer:
      </p>
<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>
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">p</span><span class="special">)</span>
    <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>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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>
</h3></div></div></div>
<h5>
<a name="core.explicit_operator_bool.history.h0"></a>
        <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
        1.56</a>
      </h5>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            Added new macros <code class="computeroutput"><span class="identifier">BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT</span></code>
            and <code class="computeroutput"><span class="identifier">BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL</span></code>
            to define <code class="computeroutput"><span class="keyword">noexcept</span></code> and
            <code class="computeroutput"><span class="keyword">constexpr</span></code> operators.
          </li>
<li class="listitem">
            The header moved to Boost.Core.
          </li>
</ul></div>
<h5>
<a name="core.explicit_operator_bool.history.h1"></a>
        <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
        1.55</a>
      </h5>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
            The macro was extracted from Boost.Log.
          </li></ul></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2014 Peter Dimov<br>Copyright &#169; 2014 Glen Fernandes<br>Copyright &#169; 2014 Andrey Semashev<p>
        Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost
        Software License, Version 1.0</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="enable_if.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="ignore_unused.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>