1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>empty_value</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="demangle.html" title="demangle"> 10<link rel="next" href="enable_if.html" title="enable_if"> 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="demangle.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="enable_if.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.empty_value"></a><a class="link" href="empty_value.html" title="empty_value">empty_value</a> 28</h2></div></div></div> 29<div class="toc"><dl class="toc"> 30<dt><span class="section"><a href="empty_value.html#core.empty_value.overview">Overview</a></span></dt> 31<dt><span class="section"><a href="empty_value.html#core.empty_value.examples">Examples</a></span></dt> 32<dt><span class="section"><a href="empty_value.html#core.empty_value.reference">Reference</a></span></dt> 33</dl></div> 34<div class="simplesect"> 35<div class="titlepage"><div><div><h3 class="title"> 36<a name="idm45312829628480"></a>Authors</h3></div></div></div> 37<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 38 Glen Fernandes 39 </li></ul></div> 40</div> 41<div class="section"> 42<div class="titlepage"><div><div><h3 class="title"> 43<a name="core.empty_value.overview"></a><a class="link" href="empty_value.html#core.empty_value.overview" title="Overview">Overview</a> 44</h3></div></div></div> 45<p> 46 The header <boost/core/empty_value.hpp> provides the class template 47 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">empty_value</span></code> for library authors to conveniently 48 leverage the Empty Base Optimization to store objects of potentially empty 49 types. 50 </p> 51</div> 52<div class="section"> 53<div class="titlepage"><div><div><h3 class="title"> 54<a name="core.empty_value.examples"></a><a class="link" href="empty_value.html#core.empty_value.examples" title="Examples">Examples</a> 55</h3></div></div></div> 56<p> 57 The following example shows <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">empty_value</span></code> 58 used to create a type that stores a pointer, comparer, and allocator, where 59 the comparer and allocator could be empty types. 60 </p> 61<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Ptr</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Compare</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Allocator</span><span class="special">></span> 62<span class="keyword">class</span> <span class="identifier">storage</span> 63 <span class="special">:</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Compare</span><span class="special">,</span> <span class="number">0</span><span class="special">></span> 64 <span class="special">,</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Allocator</span><span class="special">,</span> <span class="number">1</span><span class="special">></span> <span class="special">{</span> 65<span class="keyword">public</span><span class="special">:</span> 66 <span class="identifier">storage</span><span class="special">()</span> 67 <span class="special">:</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Compare</span><span class="special">,</span> <span class="number">0</span><span class="special">>(</span><span class="identifier">empty_init_t</span><span class="special">())</span> 68 <span class="special">,</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Allocator</span><span class="special">,</span> <span class="number">1</span><span class="special">>(</span><span class="identifier">empty_init_t</span><span class="special">())</span> 69 <span class="special">,</span> <span class="identifier">ptr_</span><span class="special">()</span> <span class="special">{</span> <span class="special">}</span> 70 71 <span class="identifier">storage</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">Compare</span><span class="special">&</span> <span class="identifier">c</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">Allocator</span><span class="special">&</span> <span class="identifier">a</span><span class="special">)</span> 72 <span class="special">:</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Compare</span><span class="special">,</span> <span class="number">0</span><span class="special">>(</span><span class="identifier">empty_init_t</span><span class="special">(),</span> <span class="identifier">c</span><span class="special">)</span> 73 <span class="special">,</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Allocator</span><span class="special">,</span> <span class="number">1</span><span class="special">>(</span><span class="identifier">empty_init_t</span><span class="special">(),</span> <span class="identifier">a</span><span class="special">)</span> 74 <span class="special">,</span> <span class="identifier">ptr_</span><span class="special">()</span> <span class="special">{</span> <span class="special">}</span> 75 76 <span class="keyword">const</span> <span class="identifier">Ptr</span><span class="special">&</span> <span class="identifier">pointer</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> 77 <span class="keyword">return</span> <span class="identifier">ptr_</span><span class="special">;</span> 78 <span class="special">}</span> 79 80 <span class="identifier">Ptr</span><span class="special">&</span> <span class="identifier">pointer</span><span class="special">()</span> <span class="special">{</span> 81 <span class="keyword">return</span> <span class="identifier">ptr_</span><span class="special">;</span> 82 <span class="special">}</span> 83 84 <span class="keyword">const</span> <span class="identifier">Compare</span><span class="special">&</span> <span class="identifier">compare</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> 85 <span class="keyword">return</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Compare</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">get</span><span class="special">();</span> 86 <span class="special">}</span> 87 88 <span class="identifier">Compare</span><span class="special">&</span> <span class="identifier">compare</span><span class="special">()</span> <span class="special">{</span> 89 <span class="keyword">return</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Compare</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">get</span><span class="special">();</span> 90 <span class="special">}</span> 91 92 <span class="keyword">const</span> <span class="identifier">Allocator</span><span class="special">&</span> <span class="identifier">allocator</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> 93 <span class="keyword">return</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Allocator</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">get</span><span class="special">();</span> 94 <span class="special">}</span> 95 96 <span class="identifier">Allocator</span><span class="special">&</span> <span class="identifier">allocator</span><span class="special">()</span> <span class="special">{</span> 97 <span class="keyword">return</span> <span class="identifier">empty_value</span><span class="special"><</span><span class="identifier">Allocator</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">get</span><span class="special">();</span> 98 <span class="special">}</span> 99 100<span class="keyword">private</span><span class="special">:</span> 101 <span class="identifier">Ptr</span> <span class="identifier">ptr_</span><span class="special">;</span> 102<span class="special">};</span> 103</pre> 104</div> 105<div class="section"> 106<div class="titlepage"><div><div><h3 class="title"> 107<a name="core.empty_value.reference"></a><a class="link" href="empty_value.html#core.empty_value.reference" title="Reference">Reference</a> 108</h3></div></div></div> 109<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> 110 111<span class="keyword">struct</span> <span class="identifier">empty_init_t</span> <span class="special">{</span> <span class="special">};</span> 112 113<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">Index</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="keyword">bool</span> <span class="identifier">Empty</span> <span class="special">=</span> <span class="emphasis"><em>see below</em></span><span class="special">></span> 114<span class="keyword">class</span> <span class="identifier">empty_value</span> <span class="special">{</span> 115<span class="keyword">public</span><span class="special">:</span> 116 <span class="keyword">typedef</span> <span class="identifier">T</span> <span class="identifier">type</span><span class="special">;</span> 117 118 <span class="identifier">empty_value</span><span class="special">()</span> <span class="special">=</span> <span class="keyword">default</span><span class="special">;</span> 119 120 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span><span class="special">...</span> <span class="identifier">Args</span><span class="special">></span> 121 <span class="identifier">empty_value</span><span class="special">(</span><span class="identifier">empty_init_t</span><span class="special">,</span> <span class="identifier">Args</span><span class="special">&&...</span> <span class="identifier">args</span><span class="special">);</span> 122 123 <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span> 124 125 <span class="identifier">T</span><span class="special">&</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">;</span> 126<span class="special">};</span> 127 128<span class="keyword">inline</span> <span class="keyword">constexpr</span> <span class="identifier">empty_init_t</span> <span class="identifier">empty_init</span><span class="special">{</span> <span class="special">};</span> 129 130<span class="special">}</span> <span class="comment">/* boost */</span> 131</pre> 132<div class="section"> 133<div class="titlepage"><div><div><h4 class="title"> 134<a name="core.empty_value.reference.template_parameters"></a><a class="link" href="empty_value.html#core.empty_value.reference.template_parameters" title="Template parameters">Template 135 parameters</a> 136</h4></div></div></div> 137<div class="variablelist"> 138<p class="title"><b></b></p> 139<dl class="variablelist"> 140<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span></code></span></dt> 141<dd><p> 142 The type of value to store 143 </p></dd> 144<dt><span class="term"><code class="computeroutput"><span class="identifier">Index</span></code></span></dt> 145<dd><p> 146 Optional: Specify to create a distinct base type 147 </p></dd> 148<dt><span class="term"><code class="computeroutput"><span class="identifier">Empty</span></code></span></dt> 149<dd><p> 150 Optional: Specify to force inheritance from type 151 </p></dd> 152</dl> 153</div> 154</div> 155<div class="section"> 156<div class="titlepage"><div><div><h4 class="title"> 157<a name="core.empty_value.reference.member_types"></a><a class="link" href="empty_value.html#core.empty_value.reference.member_types" title="Member types">Member types</a> 158</h4></div></div></div> 159<div class="variablelist"> 160<p class="title"><b></b></p> 161<dl class="variablelist"> 162<dt><span class="term"><code class="computeroutput"><span class="identifier">type</span></code></span></dt> 163<dd><p> 164 The template parameter <code class="computeroutput"><span class="identifier">T</span></code> 165 </p></dd> 166</dl> 167</div> 168</div> 169<div class="section"> 170<div class="titlepage"><div><div><h4 class="title"> 171<a name="core.empty_value.reference.constructors"></a><a class="link" href="empty_value.html#core.empty_value.reference.constructors" title="Constructors">Constructors</a> 172</h4></div></div></div> 173<div class="variablelist"> 174<p class="title"><b></b></p> 175<dl class="variablelist"> 176<dt><span class="term"><code class="computeroutput"><span class="identifier">empty_value</span><span class="special">()</span> 177 <span class="special">=</span> <span class="keyword">default</span><span class="special">;</span></code></span></dt> 178<dd><p> 179 Default initialize the value 180 </p></dd> 181<dt><span class="term"><code class="computeroutput"><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span><span class="special">...</span> <span class="identifier">Args</span><span class="special">></span> 182 <span class="identifier">empty_value</span><span class="special">(</span><span class="identifier">empty_init_t</span><span class="special">,</span> 183 <span class="identifier">Args</span><span class="special">&&...</span> 184 <span class="identifier">args</span><span class="special">);</span></code></span></dt> 185<dd><p> 186 Initialize the value with <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special"><</span><span class="identifier">Args</span><span class="special">>(</span><span class="identifier">args</span><span class="special">)...</span></code> 187 </p></dd> 188</dl> 189</div> 190</div> 191<div class="section"> 192<div class="titlepage"><div><div><h4 class="title"> 193<a name="core.empty_value.reference.member_functions"></a><a class="link" href="empty_value.html#core.empty_value.reference.member_functions" title="Member functions">Member 194 functions</a> 195</h4></div></div></div> 196<div class="variablelist"> 197<p class="title"><b></b></p> 198<dl class="variablelist"> 199<dt><span class="term"><code class="computeroutput"><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span><span class="special">;</span></code></span></dt> 200<dd><p> 201 Returns the value 202 </p></dd> 203<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span><span class="special">&</span> 204 <span class="identifier">get</span><span class="special">()</span> 205 <span class="keyword">noexcept</span><span class="special">;</span></code></span></dt> 206<dd><p> 207 Returns the value 208 </p></dd> 209</dl> 210</div> 211</div> 212</div> 213</div> 214<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 215<td align="left"></td> 216<td align="right"><div class="copyright-footer">Copyright © 2014 Peter Dimov<br>Copyright © 2014 Glen Fernandes<br>Copyright © 2014 Andrey Semashev<p> 217 Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost 218 Software License, Version 1.0</a>. 219 </p> 220</div></td> 221</tr></table> 222<hr> 223<div class="spirit-nav"> 224<a accesskey="p" href="demangle.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="enable_if.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 225</div> 226</body> 227</html> 228