1 // (C) Copyright 2002-2008, Fernando Luis Cacciola Carballal.
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // 21 Ago 2002 (Created) Fernando Cacciola
8 // 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker
9 // 23 May 2008 (Fixed operator= const issue, added initialized_value) Niels Dekker, Fernando Cacciola
10 // 21 Ago 2008 (Added swap) Niels Dekker, Fernando Cacciola
11 // 20 Feb 2009 (Fixed logical const-ness issues) Niels Dekker, Fernando Cacciola
12 // 03 Apr 2010 (Added initialized<T>, suggested by Jeffrey Hellrung, fixing #3472) Niels Dekker
13 // 30 May 2010 (Made memset call conditional, fixing #3869) Niels Dekker
15 #ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
16 #define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
18 // Note: The implementation of boost::value_initialized had to deal with the
19 // fact that various compilers haven't fully implemented value-initialization.
20 // The constructor of boost::value_initialized<T> works around these compiler
21 // issues, by clearing the bytes of T, before constructing the T object it
22 // contains. More details on these issues are at libs/utility/value_init.htm
24 #include <boost/aligned_storage.hpp>
25 #include <boost/config.hpp> // For BOOST_NO_COMPLETE_VALUE_INITIALIZATION.
26 #include <boost/detail/workaround.hpp>
27 #include <boost/static_assert.hpp>
28 #include <boost/type_traits/cv_traits.hpp>
29 #include <boost/type_traits/alignment_of.hpp>
30 #include <boost/swap.hpp>
37 // It is safe to ignore the following warning from MSVC 7.1 or higher:
38 // "warning C4351: new behavior: elements of array will be default initialized"
39 #pragma warning(disable: 4351)
40 // It is safe to ignore the following MSVC warning, which may pop up when T is
41 // a const type: "warning C4512: assignment operator could not be generated".
42 #pragma warning(disable: 4512)
46 #ifdef BOOST_NO_COMPLETE_VALUE_INITIALIZATION
47 // Implementation detail: The macro BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
48 // suggests that a workaround should be applied, because of compiler issues
49 // regarding value-initialization.
50 #define BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
53 // Implementation detail: The macro BOOST_DETAIL_VALUE_INIT_WORKAROUND
54 // switches the value-initialization workaround either on or off.
55 #ifndef BOOST_DETAIL_VALUE_INIT_WORKAROUND
56 #ifdef BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
57 #define BOOST_DETAIL_VALUE_INIT_WORKAROUND 1
59 #define BOOST_DETAIL_VALUE_INIT_WORKAROUND 0
71 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
74 remove_const<T>::type data;
82 wrapper(T const & arg)
90 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
93 aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x;
95 wrapper * wrapper_address() const
97 return static_cast<wrapper *>( static_cast<void*>(&x));
104 #if BOOST_DETAIL_VALUE_INIT_WORKAROUND
105 std::memset(&x, 0, sizeof(x));
107 new (wrapper_address()) wrapper();
110 initialized(initialized const & arg)
112 new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
115 explicit initialized(T const & arg)
117 new (wrapper_address()) wrapper(arg);
120 initialized & operator=(initialized const & arg)
122 // Assignment is only allowed when T is non-const.
123 BOOST_STATIC_ASSERT( ! is_const<T>::value );
124 *wrapper_address() = static_cast<wrapper const &>(*(arg.wrapper_address()));
130 wrapper_address()->wrapper::~wrapper();
133 T const & data() const
135 return wrapper_address()->data;
140 return wrapper_address()->data;
143 void swap(initialized & arg)
145 ::boost::swap( this->data(), arg.data() );
148 operator T const &() const
150 return wrapper_address()->data;
155 return wrapper_address()->data;
161 T const& get ( initialized<T> const& x )
167 T& get ( initialized<T>& x )
173 void swap ( initialized<T> & lhs, initialized<T> & rhs )
179 class value_initialized
183 // initialized<T> does value-initialization by default.
184 initialized<T> m_data;
193 T const & data() const
195 return m_data.data();
200 return m_data.data();
203 void swap(value_initialized & arg)
205 m_data.swap(arg.m_data);
208 operator T const &() const
221 T const& get ( value_initialized<T> const& x )
227 T& get ( value_initialized<T>& x )
233 void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs )
239 class initialized_value_t
243 template <class T> operator T() const
245 return initialized<T>().data();
249 initialized_value_t const initialized_value = {} ;