Boost logo

Boost-Commit :

From: nielsdekker_at_[hidden]
Date: 2008-01-16 04:37:26


Author: niels_dekker
Date: 2008-01-16 04:37:25 EST (Wed, 16 Jan 2008)
New Revision: 42816
URL: http://svn.boost.org/trac/boost/changeset/42816

Log:
Added test and documentation for convenience class initialized_value, that was added with changeset [42815]
Text files modified:
   trunk/libs/utility/value_init.htm | 52 ++++++++++++++++++++++++++++++++++++++-
   trunk/libs/utility/value_init_test.cpp | 8 ++++-
   2 files changed, 56 insertions(+), 4 deletions(-)

Modified: trunk/libs/utility/value_init.htm
==============================================================================
--- trunk/libs/utility/value_init.htm (original)
+++ trunk/libs/utility/value_init.htm 2008-01-16 04:37:25 EST (Wed, 16 Jan 2008)
@@ -32,7 +32,8 @@
 </dl>
                    
 <ul>
- <li>value_initialized<T></li>
+ <li>template class value_initialized<T></li>
+ <li>class initialized_value</li>
                    
 </ul>
               <a href="#acknowledgements">Acknowledgements</a><br>
@@ -52,6 +53,9 @@
 Moreover, <code>value_initialized</code> offers a workaround to various
 compiler issues regarding value-initialization.
 
+Furthermore a convenience class, <code>initialized_value</code> is provided,
+to avoid repeating the type name when retrieving the value from a
+<code>value_initialized&lt;T&gt;</code> object.
 <br>
   </p>
         
@@ -118,6 +122,16 @@
   value_initialized&lt;T&gt; var;
 </pre>
 </p>
+<p>
+The convenience class initialized_value
+allows value-initializing a variable as follows:
+<pre>
+ T var = initialized_value();
+</pre>
+This form of initialization is also very similar to <code>T4 var4 = T4()</code>,
+but robust against the aforementioned compiler issues.
+
+</p>
 
 <h2><a name="details"></a>Details</h2>
 <p>The C++ standard [3] contains the definitions
@@ -297,6 +311,40 @@
 the wrapped object is always performed with the <code>get()</code> idiom:</p>
                    
 <pre>value_initialized&lt;int&gt; x ;<br>get(x) = 1 ; // OK<br><br>value_initialized&lt;int const&gt; cx ;<br>get(x) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized&lt;int&gt; const x_c ;<br>get(x_c) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized&lt;int const&gt; const cx_c ;<br>get(cx_c) = 1 ; // ERROR: Cannot modify a const object<br></pre>
+
+<h2><a name="initialized_value"><code>class initialized_value</code></a></h2>
+
+<pre>
+namespace boost {
+class initialized_value
+{
+ public :
+ template &lt;class T&gt; operator T() const ;
+};
+} // namespace boost
+</pre>
+
+The class <code>initialized_value</code> provides a convenient way to get
+an initialized value: its conversion operator provides an appropriate
+<em>value-initialized</em> object for any CopyConstructible type.
+
+Suppose you need to have an initialized variable of type <code>T</code>.
+You could do it as follows:
+<pre>
+ T var = T();
+</pre>
+But as mentioned before, this form suffers from various compiler issues.
+The template <code>value_initialized</code> offers a workaround:
+<pre>
+ T var = get( value_initialized&lt;T&gt;() );
+</pre>
+Unfortunately both forms repeat the type name, which
+is rather short now (<code>T</code>), but could of course be
+more like <code>Namespace::Template&lt;Arg&gt;::Type</code>.
+Instead, one could use <code>initialized_value</code> as follows:
+<pre>
+ T var = initialized_value();
+</pre>
                     
 <h3><a name="references">References</a></h3>
           [1] Bjarne Stroustrup, Gabriel Dos Reis, and J. Stephen Adamczyk wrote
@@ -326,7 +374,7 @@
      </p>
                     
 <hr>
-<p>Revised 15 January 2008</p>
+<p>Revised 16 January 2008</p>
                    
 <p>&copy; Copyright Fernando Cacciola, 2002, 2008.</p>
                    

Modified: trunk/libs/utility/value_init_test.cpp
==============================================================================
--- trunk/libs/utility/value_init_test.cpp (original)
+++ trunk/libs/utility/value_init_test.cpp 2008-01-16 04:37:25 EST (Wed, 16 Jan 2008)
@@ -7,7 +7,7 @@
 // Test program for "boost/utility/value_init.hpp"
 //
 // 21 Agu 2002 (Created) Fernando Cacciola
-// 15 Jan 2008 (Added tests regarding compiler issues) Fernando Cacciola, Niels Dekker
+// 16 Jan 2008 (Added tests regarding compiler issues and initialized_value) Fernando Cacciola, Niels Dekker
 
 #include <cstring> // For memcmp.
 #include <iostream>
@@ -52,7 +52,7 @@
 struct NonPOD : NonPODBase
 {
   NonPOD () : id() {}
- NonPOD ( std::string const& id_) : id(id_) {}
+ explicit NonPOD ( std::string const& id_) : id(id_) {}
 
   friend std::ostream& operator << ( std::ostream& os, NonPOD const& npod )
     { return os << '(' << npod.id << ')' ; }
@@ -192,6 +192,10 @@
   boost::value_initialized<T> x ;
   BOOST_CHECK ( y == x ) ;
   BOOST_CHECK ( y == boost::get(x) ) ;
+
+ T initializedValue = boost::initialized_value() ;
+ BOOST_CHECK ( y == initializedValue ) ;
+
   static_cast<T&>(x) = z ;
   boost::get(x) = z ;
   BOOST_CHECK ( x == z ) ;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk