From 9e73b2c6aebf08fd6b61c39236813ab60ad1cc51 Mon Sep 17 00:00:00 2001
From: Niels Dekker An object of this template class is a Both When
-template class value_initialized<T>
namespace boost {
+
template<class T>
class value_initialized
{
public :
value_initialized() : x() {}
operator T&() const { return x ; }
T& data() const { return x ; }
private :
unspecified x ;
} ;
template<class T>
T const& get ( value_initialized<T> const& x )
{
return x.data() ;
}
template<class T>
T& get ( value_initialized<T>& x )
{
return x.data() ;
}
} // namespace boostnamespace boost {
template<class T>
class value_initialized
{
public :
value_initialized() : x() {}
operator T&() const { return x ; }
T& data() const { return x ; }
void swap( value_initialized<T>& );
private :
unspecified x ;
} ;
template<class T>
T const& get ( value_initialized<T> const& x )
{
return x.data() ;
}
template<class T>
T& get ( value_initialized<T>& x )
{
return x.data() ;
}
} // namespace boostT
-wrapper convertible
to 'T&'
whose wrapped object (data member of type T
)
@@ -276,6 +276,10 @@ non-member function get()
: const
and non-const
objects can be wrapped.
Mutable objects can be modified directly from within the wrapper but constant
objects cannot:T
is a Swappable type, value_initialized<T>
+ is swappable as well, by calling its swap
member function
+ as well as by calling boost::swap
.value_initialized<int> x ;
@@ -311,7 +315,7 @@ wrapped object from within a constant wrapper can be avoided if access to
the wrapped object is always performed with the
static_cast<int&>(x) = 1 ; // OK
get(x) = 1 ; // OK
value_initialized<int const> y ;
static_cast<int&>(y) = 1 ; // ERROR: cannot cast to int&
static_cast<int const&>(y) = 1 ; // ERROR: cannot modify a const value
get(y) = 1 ; // ERROR: cannot modify a const valueget()
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
@@ -379,7 +383,7 @@ for Boost release version 1.35 (2008), offering a workaround to various compiler
-Revised 23 May 2008
+Revised 28 August 2008
© Copyright Fernando Cacciola, 2002, 2008.
@@ -390,4 +394,4 @@ for Boost release version 1.35 (2008), offering a workaround to various compiler