From 8827b8ed8b79a1ae9f93b1fcd745ae4b57349184 Mon Sep 17 00:00:00 2001
From: Niels Dekker
@@ -52,6 +53,9 @@ union and class types.
Moreover, value_initialized
offers a workaround to various
compiler issues regarding value-initialization.
+Furthermore, a const
object, initialized_value
is provided,
+to avoid repeating the type name when retrieving the value from a
+value_initialized<T>
object.
value_initialized<T> var;+ +
+The const
object initialized_value
+allows value-initializing a variable as follows:
+
+ T var = initialized_value ; ++This form of initialization is semantically equivalent to
T4 var4 = T4()
,
+but robust against the aforementioned compiler issues.
+
value_initialized
will now clea
its internal data, prior to constructing the object that it contains.
-template class value_initialized<T>
get()
idiom:
value_initialized<int> x ;+
get(x) = 1 ; // OK
value_initialized<int const> cx ;
get(x) = 1 ; // ERROR: Cannot modify a const object
value_initialized<int> const x_c ;
get(x_c) = 1 ; // ERROR: Cannot modify a const object
value_initialized<int const> const cx_c ;
get(cx_c) = 1 ; // ERROR: Cannot modify a const object
initialized_value
+namespace boost { +class initialized_value_t +{ + public : + template <class T> operator T() const ; +}; + +initialized_value_t const initialized_value = {} ; + +} // namespace boost ++ +
initialized_value
provides a convenient way to get
+an initialized value: its conversion operator provides an appropriate
+value-initialized object for any CopyConstructible type.
+
+Suppose you need to have an initialized variable of type T
.
+You could do it as follows:
++ T var = T(); ++But as mentioned before, this form suffers from various compiler issues. +The template
value_initialized
offers a workaround:
++ T var = get( value_initialized<T>() ); ++Unfortunately both forms repeat the type name, which +is rather short now (
T
), but could of course be
+more like Namespace::Template<Arg>::Type
.
+Instead, one could use initialized_value
as follows:
++ T var = initialized_value ; ++
value_initialized was reimplemented by Fernando Cacciola and Niels Dekker for Boost release version 1.35 (2008), offering a workaround to various compiler issues.
+initialized_value was written by Niels Dekker, and added to Boost release version 1.36 (2008). +
Developed by Fernando Cacciola, the latest version of this file can be found at www.boost.org.
Revised 15 January 2008
+Revised 23 May 2008
© Copyright Fernando Cacciola, 2002, 2008.
@@ -337,4 +390,4 @@ for Boost release version 1.35 (2008), offering a workaround to various compiler