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
13 #ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
14 #define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
16 // Note: The implementation of boost::value_initialized had to deal with the
17 // fact that various compilers haven't fully implemented value-initialization.
18 // The constructor of boost::value_initialized<T> works around these compiler
19 // issues, by clearing the bytes of T, before constructing the T object it
20 // contains. More details on these issues are at libs/utility/value_init.htm
22 #include <boost/aligned_storage.hpp>
23 #include <boost/detail/workaround.hpp>
24 #include <boost/static_assert.hpp>
25 #include <boost/type_traits/cv_traits.hpp>
26 #include <boost/type_traits/alignment_of.hpp>
27 #include <boost/swap.hpp>
34 class value_initialized
39 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
42 remove_const<T>::type data;
46 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
49 aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x;
51 wrapper * wrapper_address() const
53 return static_cast<wrapper *>( static_cast<void*>(&x));
60 std::memset(&x, 0, sizeof(x));
64 // When using MSVC 7.1 or higher, the following placement new expression may trigger warning C4345:
65 // "behavior change: an object of POD type constructed with an initializer of the form ()
66 // will be default-initialized". It is safe to ignore this warning when using value_initialized.
67 #pragma warning(disable: 4345)
70 new (wrapper_address()) wrapper();
76 value_initialized(value_initialized const & arg)
78 new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
81 value_initialized & operator=(value_initialized const & arg)
83 // Assignment is only allowed when T is non-const.
84 BOOST_STATIC_ASSERT( ! is_const<T>::value );
85 *wrapper_address() = static_cast<wrapper const &>(*(arg.wrapper_address()));
91 wrapper_address()->wrapper::~wrapper();
94 T const & data() const
96 return wrapper_address()->data;
101 return wrapper_address()->data;
104 void swap(value_initialized & arg)
106 ::boost::swap( this->data(), arg.data() );
109 operator T const &() const { return this->data(); }
111 operator T&() { return this->data(); }
118 T const& get ( value_initialized<T> const& x )
123 T& get ( value_initialized<T>& x )
129 void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs )
135 class initialized_value_t
139 template <class T> operator T() const
141 return get( value_initialized<T>() );
145 initialized_value_t const initialized_value = {} ;