From 6b6e1c3252e018d13e51ef57d7a597bcd7b7ce3f 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 ;
@@ -379,7 +383,7 @@ for Boost release version 1.35 (2008), offering a workaround to various compiler
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 value
Revised 23 May 2008
+Revised 28 August 2008
© Copyright Fernando Cacciola, 2002, 2008.
diff --git a/value_init_test.cpp b/value_init_test.cpp index 7b07b22..63f324d 100644 --- a/value_init_test.cpp +++ b/value_init_test.cpp @@ -9,6 +9,7 @@ // 21 Ago 2002 (Created) Fernando Cacciola // 15 Jan 2008 (Added tests regarding compiler issues) Fernando Cacciola, Niels Dekker // 23 May 2008 (Added tests regarding initialized_value) Niels Dekker +// 21 Ago 2008 (Added swap test) Niels Dekker #include