Boost C++ Libraries: Ticket #5172: Boost Optional enhancement https://svn.boost.org/trac10/ticket/5172 <p> Boost.Optional is a very handy class.<br /> It can be extended to allow more flexibility. <br /> Proposal:<br /> Add additional argument with size to optional.<br /> Then the storage is created with the given size without checking sizeof(T).<br /> And only in the construct function the size is being checked.<br /> If the size is less than sizeof(T) , give error.<br /> If the size is much more, give a warning. <br /> (say 3 times or depending on the given size, e.g. for 1-2: 32 times, for 3-16: 8 times, for 17-32: 4 times, for 33-infinite: 3 times..)<br /> <br /> </p> <p> So we can have this code: </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/optional2.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/utility/in_place_factory.hpp&gt;</span><span class="cp"></span> <span class="k">class</span> <span class="nc">x</span><span class="p">;</span> <span class="c1">// Class declaration, Incomplete type!</span> <span class="k">class</span> <span class="nc">y</span><span class="p">;</span> <span class="c1">// Class declaration, Incomplete type!</span> <span class="k">class</span> <span class="nc">z</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="n">z</span><span class="p">(</span><span class="kt">char</span><span class="p">)</span> <span class="p">{}</span> <span class="p">};</span> <span class="c1">// Full type</span> <span class="k">class</span> <span class="nc">a</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="n">a</span><span class="p">();</span> <span class="o">~</span><span class="n">a</span><span class="p">();</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional2</span><span class="o">&lt;</span><span class="n">x</span><span class="p">,</span> <span class="mi">10</span><span class="o">&gt;</span> <span class="n">x_</span><span class="p">;</span> <span class="c1">// incomplete type</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional2</span><span class="o">&lt;</span><span class="n">y</span><span class="p">,</span> <span class="mi">20</span><span class="o">&gt;</span> <span class="n">y_</span><span class="p">;</span> <span class="c1">// incomplete type</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional2</span><span class="o">&lt;</span><span class="n">z</span><span class="o">&gt;</span> <span class="n">z_</span><span class="p">;</span> <span class="c1">// sizeof(T) like in old boost::optional</span> <span class="p">};</span> <span class="k">class</span> <span class="nc">x</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="n">x</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="p">{}</span> <span class="p">};</span> <span class="k">class</span> <span class="nc">y</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="n">y</span><span class="p">(</span><span class="kt">double</span><span class="p">)</span> <span class="p">{}</span> <span class="kt">char</span> <span class="n">c</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span> <span class="p">};</span> <span class="n">a</span><span class="o">::</span><span class="n">a</span><span class="p">()</span> <span class="p">{</span> <span class="c1">// Call constructors in lazy way</span> <span class="n">x_</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">in_place</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="n">y_</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">in_place</span><span class="p">(</span><span class="mf">2.0</span><span class="p">);</span> <span class="n">z_</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">in_place</span><span class="p">(</span><span class="sc">&#39;c&#39;</span><span class="p">);</span> <span class="p">}</span> <span class="n">a</span><span class="o">::~</span><span class="n">a</span><span class="p">()</span> <span class="p">{}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="p">}</span> </pre></div></div><p> Full code resides here:<br /> <a class="ext-link" href="http://pastebin.com/4db0RFC8"><span class="icon">​</span>http://pastebin.com/4db0RFC8</a> <br /> <a class="ext-link" href="http://ideone.com/mBFVi"><span class="icon">​</span>http://ideone.com/mBFVi</a> <br /> <br /> In short, it just adds std::size_t N to the optional. And then chooses the right size in aligned_storage. </p> <div class="wiki-code"><div class="code"><pre> <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="kt">size_t</span> <span class="n">N</span><span class="o">&gt;</span> <span class="k">class</span> <span class="nc">aligned_storage</span> <span class="p">{</span> <span class="c1">// Borland ICEs if unnamed unions are used for this!</span> <span class="k">union</span> <span class="n">dummy_u_helper</span> <span class="p">{</span> <span class="kt">char</span> <span class="n">data</span><span class="p">[</span> <span class="n">N</span> <span class="p">];</span> <span class="p">}</span> <span class="n">dummy_helper</span> <span class="p">;</span> <span class="k">union</span> <span class="n">dummy_u</span> <span class="p">{</span> <span class="kt">char</span> <span class="n">data</span><span class="p">[</span> <span class="n">N</span> <span class="p">];</span> <span class="n">BOOST_DEDUCED_TYPENAME</span> <span class="n">type_with_alignment</span><span class="o">&lt;</span> <span class="o">::</span><span class="n">boost</span><span class="o">::</span><span class="n">alignment_of</span><span class="o">&lt;</span><span class="n">dummy_u_helper</span><span class="o">&gt;::</span><span class="n">value</span> <span class="o">&gt;::</span><span class="n">type</span> <span class="n">aligner_</span><span class="p">;</span> <span class="p">}</span> <span class="n">dummy_</span> <span class="p">;</span> <span class="k">public</span><span class="o">:</span> <span class="kt">void</span> <span class="k">const</span><span class="o">*</span> <span class="n">address</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="o">&amp;</span><span class="n">dummy_</span><span class="p">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="p">}</span> <span class="kt">void</span> <span class="o">*</span> <span class="n">address</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="o">&amp;</span><span class="n">dummy_</span><span class="p">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="p">;</span> <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span> <span class="k">class</span> <span class="nc">aligned_storage</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="mi">0</span><span class="o">&gt;</span> <span class="p">{</span> <span class="c1">// Borland ICEs if unnamed unions are used for this!</span> <span class="k">union</span> <span class="n">dummy_u</span> <span class="p">{</span> <span class="kt">char</span> <span class="n">data</span><span class="p">[</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">T</span><span class="p">)</span> <span class="p">];</span> <span class="n">BOOST_DEDUCED_TYPENAME</span> <span class="n">type_with_alignment</span><span class="o">&lt;</span> <span class="o">::</span><span class="n">boost</span><span class="o">::</span><span class="n">alignment_of</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;::</span><span class="n">value</span> <span class="o">&gt;::</span><span class="n">type</span> <span class="n">aligner_</span><span class="p">;</span> <span class="p">}</span> <span class="n">dummy_</span> <span class="p">;</span> <span class="k">public</span><span class="o">:</span> <span class="kt">void</span> <span class="k">const</span><span class="o">*</span> <span class="n">address</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="o">&amp;</span><span class="n">dummy_</span><span class="p">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="p">}</span> <span class="kt">void</span> <span class="o">*</span> <span class="n">address</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="o">&amp;</span><span class="n">dummy_</span><span class="p">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="p">;</span> </pre></div></div><p> I have not added check in the link above but it is easy to add it using BOOST_STATIC_ASSERT and BOOST_STATIC_WARNING (which resides in boost/serialization/static_warning.hpp) </p> <p> Thanks ! </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5172 Trac 1.4.3 nn-mail@… Wed, 09 Feb 2011 12:33:08 GMT attachment set https://svn.boost.org/trac10/ticket/5172 https://svn.boost.org/trac10/ticket/5172 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">optional2.h</span> </li> </ul> <p> Optional proposal file. In case links don't work. </p> Ticket Steven Watanabe Thu, 10 Feb 2011 18:35:59 GMT <link>https://svn.boost.org/trac10/ticket/5172#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5172#comment:1</guid> <description> <p> This shouldn't be part of optional. It's functionality is orthogonal to that of optional. </p> <div class="wiki-code"><div class="code"><pre><span class="c1">// Allows storing T, but does not require T to be</span> <span class="c1">// a complete type except in construct/copy/destroy operations.</span> <span class="c1">// Requires: size &gt;= sizeof(T)</span> <span class="c1">// size % alignment == 0</span> <span class="c1">// alignment % alignment_of&lt;T&gt;::value == 0</span> <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="kt">size_t</span> <span class="n">alignment</span><span class="o">&gt;</span> <span class="k">class</span> <span class="nc">typed_buffer</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="p">...</span><span class="o">&gt;</span> <span class="n">typed_buffer</span><span class="p">(</span><span class="n">T</span><span class="o">&amp;&amp;</span> <span class="n">t</span><span class="p">...);</span> <span class="o">~</span><span class="n">typed_buffer</span><span class="p">();</span> <span class="n">typed_buffer</span><span class="o">&amp;</span> <span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="k">const</span> <span class="n">typed_buffer</span><span class="o">&amp;</span><span class="p">);</span> <span class="n">T</span><span class="o">&amp;</span> <span class="n">get</span><span class="p">();</span> <span class="k">const</span> <span class="n">T</span><span class="o">&amp;</span> <span class="n">get</span><span class="p">()</span> <span class="k">const</span><span class="p">;</span> <span class="k">private</span><span class="o">:</span> <span class="n">boost</span><span class="o">::</span><span class="n">aligned_storage</span><span class="o">&lt;</span><span class="n">size</span><span class="p">,</span> <span class="n">alignment</span><span class="o">&gt;</span> <span class="n">_storage</span><span class="p">;</span> <span class="p">};</span> </pre></div></div> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Thu, 07 Apr 2011 21:16:49 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5172#comment:2 https://svn.boost.org/trac10/ticket/5172#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> Ticket