Merged value_init.hpp from trunk, including new boost::initialized_value object. (Was discussed beforehand with Fernando Cacciola.)

[SVN r46464]
This commit is contained in:
Niels Dekker
2008-06-17 22:14:09 +00:00
parent 50bc75a802
commit 8849fbc52d

View File

@ -1,4 +1,4 @@
// (C) Copyright 2002-2007, Fernando Luis Cacciola Carballal. // (C) Copyright 2002-2008, Fernando Luis Cacciola Carballal.
// //
// Distributed under the Boost Software License, Version 1.0. (See // Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at // accompanying file LICENSE_1_0.txt or copy at
@ -6,29 +6,20 @@
// //
// 21 Ago 2002 (Created) Fernando Cacciola // 21 Ago 2002 (Created) Fernando Cacciola
// 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker // 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker
// 23 May 2008 (Fixed operator= const issue, added initialized_value) Niels Dekker, Fernando Cacciola
// //
#ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP #ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
#define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP #define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
// Note: The implementation of boost::value_initialized had to deal with the // Note: The implementation of boost::value_initialized had to deal with the
// fact that various compilers haven't fully implemented value-initialization: // fact that various compilers haven't fully implemented value-initialization.
// Microsoft Feedback ID 100744 - Value-initialization in new-expression // The constructor of boost::value_initialized<T> works around these compiler
// Reported by Pavel Kuznetsov (MetaCommunications Engineering), 2005-07-28 // issues, by clearing the bytes of T, before constructing the T object it
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744 // contains. More details on these issues are at libs/utility/value_init.htm
// GCC Bug 30111 - Value-initialization of POD base class doesn't initialize members
// Reported by Jonathan Wakely, 2006-12-07
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30111
// GCC Bug 33916 - Default constructor fails to initialize array members
// Reported by Michael Elizabeth Chastain, 2007-10-26
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33916
// Borland Report 51854 - Value-initialization: POD struct should be zero-initialized
// Reported by Niels Dekker (LKEB, Leiden University Medical Center), 2007-11-09
// http://qc.codegear.com/wc/qcmain.aspx?d=51854
// The constructor of boost::value_initialized<T> works around these issues, by
// clearing the bytes of T, before constructing the T object it contains.
#include <boost/aligned_storage.hpp> #include <boost/aligned_storage.hpp>
#include <boost/detail/workaround.hpp> #include <boost/detail/workaround.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/cv_traits.hpp> #include <boost/type_traits/cv_traits.hpp>
#include <boost/type_traits/alignment_of.hpp> #include <boost/type_traits/alignment_of.hpp>
#include <cstring> #include <cstring>
@ -48,23 +39,32 @@ class value_initialized
remove_const<T>::type data; remove_const<T>::type data;
}; };
mutable aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value> x; mutable
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
typename
#endif
aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x;
wrapper * wrapper_address() const
{
return static_cast<wrapper *>( static_cast<void*>(&x));
}
public : public :
value_initialized() value_initialized()
{ {
std::memset(x.address(), 0, sizeof(x)); std::memset(&x, 0, sizeof(x));
#ifdef BOOST_MSVC #ifdef BOOST_MSVC
#pragma warning(push) #pragma warning(push)
#if _MSC_VER >= 1310 #if _MSC_VER >= 1310
// When using MSVC 7.1 or higher, the following placement new expression may trigger warning C4345: // When using MSVC 7.1 or higher, the following placement new expression may trigger warning C4345:
// "behavior change: an object of POD type constructed with an initializer of the form () // "behavior change: an object of POD type constructed with an initializer of the form ()
// will be default-initialized". There is no need to worry about this, though. // will be default-initialized". It is safe to ignore this warning when using value_initialized.
#pragma warning(disable: 4345) #pragma warning(disable: 4345)
#endif #endif
#endif #endif
new (x.address()) wrapper(); new (wrapper_address()) wrapper();
#ifdef BOOST_MSVC #ifdef BOOST_MSVC
#pragma warning(pop) #pragma warning(pop)
#endif #endif
@ -72,25 +72,25 @@ class value_initialized
value_initialized(value_initialized const & arg) value_initialized(value_initialized const & arg)
{ {
new (x.address()) wrapper( *static_cast<wrapper const *>(arg.x.address()) ); new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
} }
value_initialized & operator=(value_initialized const & arg) value_initialized & operator=(value_initialized const & arg)
{ {
T & this_data = this->data(); // Assignment is only allowed when T is non-const.
T const & arg_data = arg.data(); BOOST_STATIC_ASSERT( ! is_const<T>::value );
this_data = arg_data; *wrapper_address() = static_cast<wrapper const &>(*(arg.wrapper_address()));
return *this; return *this;
} }
~value_initialized() ~value_initialized()
{ {
static_cast<wrapper *>(x.address())->wrapper::~wrapper(); wrapper_address()->wrapper::~wrapper();
} }
T& data() const T& data() const
{ {
return static_cast<wrapper *>(x.address())->data; return wrapper_address()->data;
} }
operator T&() const { return this->data(); } operator T&() const { return this->data(); }
@ -110,6 +110,20 @@ T& get ( value_initialized<T>& x )
return x.data() ; return x.data() ;
} }
class initialized_value_t
{
public :
template <class T> operator T() const
{
return get( value_initialized<T>() );
}
};
initialized_value_t const initialized_value = {} ;
} // namespace boost } // namespace boost