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>
36 // It is safe to ignore the following warning from MSVC 7.1 or higher:
37 // "warning C4351: new behavior: elements of array will be default initialized"
38 #pragma warning(disable: 4351)
39 // It is safe to ignore the following MSVC warning, which may pop up when T is
40 // a const type: "warning C4512: assignment operator could not be generated".
41 #pragma warning(disable: 4512)
44 #ifdef BOOST_NO_COMPLETE_VALUE_INITIALIZATION
45 // Implementation detail: The macro BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
46 // suggests that a workaround should be applied, because of compiler issues
47 // regarding value-initialization.
48 #define BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
51 // Implementation detail: The macro BOOST_DETAIL_VALUE_INIT_WORKAROUND
52 // switches the value-initialization workaround either on or off.
53 #ifndef BOOST_DETAIL_VALUE_INIT_WORKAROUND
54 #ifdef BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
55 #define BOOST_DETAIL_VALUE_INIT_WORKAROUND 1
57 #define BOOST_DETAIL_VALUE_INIT_WORKAROUND 0
69 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
72 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;
96 wrapper * wrapper_address() const
98 return static_cast<wrapper *>( static_cast<void*>(&x));
106 #if BOOST_DETAIL_VALUE_INIT_WORKAROUND
107 std::memset(&x, 0, sizeof(x));
109 new (wrapper_address()) wrapper();
113 initialized(initialized const & arg)
115 new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
119 explicit initialized(T const & arg)
121 new (wrapper_address()) wrapper(arg);
125 initialized & operator=(initialized const & arg)
127 // Assignment is only allowed when T is non-const.
128 BOOST_STATIC_ASSERT( ! is_const<T>::value );
129 *wrapper_address() = static_cast<wrapper const &>(*(arg.wrapper_address()));
136 wrapper_address()->wrapper::~wrapper();
140 T const & data() const
142 return wrapper_address()->data;
148 return wrapper_address()->data;
152 void swap(initialized & arg)
154 ::boost::swap( this->data(), arg.data() );
158 operator T const &() const
160 return wrapper_address()->data;
166 return wrapper_address()->data;
173 T const& get ( initialized<T> const& x )
180 T& get ( initialized<T>& x )
187 void swap ( initialized<T> & lhs, initialized<T> & rhs )
193 class value_initialized
197 // initialized<T> does value-initialization by default.
198 initialized<T> m_data;
209 T const & data() const
211 return m_data.data();
217 return m_data.data();
221 void swap(value_initialized & arg)
223 m_data.swap(arg.m_data);
227 operator T const &() const
242 T const& get ( value_initialized<T> const& x )
249 T& get ( value_initialized<T>& x )
256 void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs )
262 class initialized_value_t
266 template <class T> BOOST_GPU_ENABLED operator T() const
268 return initialized<T>().data();
272 initialized_value_t const initialized_value = {} ;