2007-09-07 17:17:09 +00:00
|
|
|
// (C) Copyright 2002-2007, Fernando Luis Cacciola Carballal.
|
2002-09-11 05:35:41 +00:00
|
|
|
//
|
2004-08-19 15:19:17 +00:00
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See
|
|
|
|
// accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
2002-09-11 05:35:41 +00:00
|
|
|
//
|
|
|
|
// 21 Ago 2002 (Created) Fernando Cacciola
|
2007-09-07 17:17:09 +00:00
|
|
|
// 07 Set 2007 (Worked around MSVC++ bug) Fernando Cacciola, Niels Dekker
|
2007-11-18 22:11:57 +00:00
|
|
|
// 16 Nov 2007 (Refactoring: removed private base classes) Fernando Cacciola, Niels Dekker
|
2007-12-09 22:49:58 +00:00
|
|
|
// 09 Dec 2007 (Worked around various compiler bugs) Fernando Cacciola, Niels Dekker
|
2002-09-11 05:35:41 +00:00
|
|
|
//
|
|
|
|
#ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
|
|
|
#define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
|
|
|
|
2007-12-09 22:49:58 +00:00
|
|
|
// Note: The implementation of boost::value_initialized had to deal with the
|
|
|
|
// fact that various compilers haven't fully implemented value-initialization:
|
|
|
|
// Microsoft Feedback ID 100744 - Value-initialization in new-expression
|
|
|
|
// Reported by Pavel Kuznetsov (MetaCommunications Engineering), 2005-07-28
|
2007-09-07 17:17:09 +00:00
|
|
|
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744
|
2007-12-09 22:49:58 +00:00
|
|
|
// 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.
|
2007-11-18 22:11:57 +00:00
|
|
|
|
2007-09-07 17:17:09 +00:00
|
|
|
#include <boost/aligned_storage.hpp>
|
2007-12-09 22:49:58 +00:00
|
|
|
#include <boost/detail/select_type.hpp>
|
|
|
|
#include <boost/detail/workaround.hpp>
|
|
|
|
#include <boost/type_traits/cv_traits.hpp>
|
2007-09-07 17:17:09 +00:00
|
|
|
#include <boost/type_traits/alignment_of.hpp>
|
|
|
|
#include <cstring>
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
|
|
|
|
template<class T>
|
2007-11-18 22:11:57 +00:00
|
|
|
class value_initialized
|
2007-09-07 17:17:09 +00:00
|
|
|
{
|
|
|
|
private :
|
2007-12-09 22:49:58 +00:00
|
|
|
struct wrapper
|
|
|
|
{
|
|
|
|
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
|
|
|
|
typename
|
|
|
|
#endif
|
|
|
|
remove_const<T>::type data;
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable
|
|
|
|
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
|
|
|
|
typename
|
|
|
|
#endif
|
|
|
|
::boost::aligned_storage<sizeof(wrapper), ::boost::alignment_of<wrapper>::value>::type x;
|
2007-09-07 17:17:09 +00:00
|
|
|
|
2007-11-18 22:11:57 +00:00
|
|
|
public :
|
2007-09-07 17:17:09 +00:00
|
|
|
|
2007-11-18 22:11:57 +00:00
|
|
|
value_initialized()
|
2007-09-07 17:17:09 +00:00
|
|
|
{
|
|
|
|
std::memset(&x, 0, sizeof(x));
|
2007-12-09 22:49:58 +00:00
|
|
|
#ifdef BOOST_MSVC
|
|
|
|
#pragma warning(push)
|
|
|
|
#if _MSC_VER >= 1310
|
|
|
|
// 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 ()
|
|
|
|
// will be default-initialized". There is no need to worry about this, though.
|
|
|
|
#pragma warning(disable: 4345)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
new (&x) wrapper();
|
|
|
|
#ifdef BOOST_MSVC
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
2007-09-07 17:17:09 +00:00
|
|
|
}
|
|
|
|
|
2007-12-09 22:49:58 +00:00
|
|
|
value_initialized(value_initialized const & arg)
|
2007-09-15 23:11:50 +00:00
|
|
|
{
|
2007-12-09 22:49:58 +00:00
|
|
|
void const * const ptr = &(arg.x);
|
|
|
|
new (&x) wrapper( *static_cast<wrapper const *>(ptr) );
|
2007-09-15 23:11:50 +00:00
|
|
|
}
|
2007-09-07 17:17:09 +00:00
|
|
|
|
2007-12-09 22:49:58 +00:00
|
|
|
value_initialized & operator=(value_initialized const & arg)
|
2007-11-18 22:11:57 +00:00
|
|
|
{
|
2007-12-09 22:49:58 +00:00
|
|
|
T & this_data = this->data();
|
|
|
|
T const & arg_data = arg.data();
|
|
|
|
this_data = arg_data;
|
|
|
|
return *this;
|
2007-09-07 17:17:09 +00:00
|
|
|
}
|
|
|
|
|
2007-12-09 22:49:58 +00:00
|
|
|
~value_initialized()
|
|
|
|
{
|
|
|
|
void * const ptr = &x;
|
|
|
|
static_cast<wrapper *>(ptr)->wrapper::~wrapper();
|
|
|
|
}
|
2002-09-11 05:35:41 +00:00
|
|
|
|
2007-12-09 22:49:58 +00:00
|
|
|
T& data() const
|
|
|
|
{
|
|
|
|
void * const ptr = &x;
|
|
|
|
return static_cast<wrapper *>(ptr)->data;
|
|
|
|
}
|
2002-09-11 05:35:41 +00:00
|
|
|
|
2007-11-18 22:11:57 +00:00
|
|
|
operator T&() const { return this->data(); }
|
2002-09-11 05:35:41 +00:00
|
|
|
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 boost
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|