1// (C) Copyright 2002-2008, Fernando Luis Cacciola Carballal.
2// Copyright 2020 Peter Dimov
3//
4// Distributed under the Boost Software License, Version 1.0. (See
5// accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7//
8// 21 Ago 2002 (Created) Fernando Cacciola
9// 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker
10// 23 May 2008 (Fixed operator= const issue, added initialized_value) Niels Dekker, Fernando Cacciola
11// 21 Ago 2008 (Added swap) Niels Dekker, Fernando Cacciola
12// 20 Feb 2009 (Fixed logical const-ness issues) Niels Dekker, Fernando Cacciola
13// 03 Apr 2010 (Added initialized<T>, suggested by Jeffrey Hellrung, fixing #3472) Niels Dekker
14// 30 May 2010 (Made memset call conditional, fixing #3869) Niels Dekker
15//
16#ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
17#define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
18
19// Note: The implementation of boost::value_initialized had to deal with the
20// fact that various compilers haven't fully implemented value-initialization.
21// The constructor of boost::value_initialized<T> works around these compiler
22// issues, by clearing the bytes of T, before constructing the T object it
23// contains. More details on these issues are at libs/utility/value_init.htm
24
25#include <boost/config.hpp> // For BOOST_NO_COMPLETE_VALUE_INITIALIZATION.
26#include <boost/core/swap.hpp>
27#include <cstring>
28#include <cstddef>
29
30#ifdef BOOST_MSVC
31#pragma warning(push)
32// It is safe to ignore the following warning from MSVC 7.1 or higher:
33// "warning C4351: new behavior: elements of array will be default initialized"
34#pragma warning(disable: 4351)
35// It is safe to ignore the following MSVC warning, which may pop up when T is
36// a const type: "warning C4512: assignment operator could not be generated".
37#pragma warning(disable: 4512)
38#endif
39
40#ifndef BOOST_UTILITY_DOCS
41
42#ifdef BOOST_NO_COMPLETE_VALUE_INITIALIZATION
43 // Implementation detail: The macro BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
44 // suggests that a workaround should be applied, because of compiler issues
45 // regarding value-initialization.
46 #define BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
47#endif
48
49// Implementation detail: The macro BOOST_DETAIL_VALUE_INIT_WORKAROUND
50// switches the value-initialization workaround either on or off.
51#ifndef BOOST_DETAIL_VALUE_INIT_WORKAROUND
52 #ifdef BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
53 #define BOOST_DETAIL_VALUE_INIT_WORKAROUND 1
54 #else
55 #define BOOST_DETAIL_VALUE_INIT_WORKAROUND 0
56 #endif
57#endif
58
59#endif // BOOST_UTILITY_DOCS
60
61namespace boost {
62
63namespace detail {
64
65 struct zero_init
66 {
67 zero_init()
68 {
69 }
70
71 zero_init( void * p, std::size_t n )
72 {
73 std::memset( s: p, c: 0, n: n );
74 }
75 };
76
77} // namespace detail
78
79template<class T>
80class initialized
81#if BOOST_DETAIL_VALUE_INIT_WORKAROUND
82 : detail::zero_init
83#endif
84{
85 private:
86
87 T data_;
88
89 public :
90
91 BOOST_GPU_ENABLED
92 initialized():
93#if BOOST_DETAIL_VALUE_INIT_WORKAROUND
94 zero_init( &const_cast< char& >( reinterpret_cast<char const volatile&>( data_ ) ), sizeof( data_ ) ),
95#endif
96 data_()
97 {
98 }
99
100 BOOST_GPU_ENABLED
101 explicit initialized(T const & arg): data_( arg )
102 {
103 }
104
105 BOOST_GPU_ENABLED
106 T const & data() const
107 {
108 return data_;
109 }
110
111 BOOST_GPU_ENABLED
112 T& data()
113 {
114 return data_;
115 }
116
117 BOOST_GPU_ENABLED
118 void swap(initialized & arg)
119 {
120 ::boost::swap( this->data(), arg.data() );
121 }
122
123 BOOST_GPU_ENABLED
124 operator T const &() const
125 {
126 return data_;
127 }
128
129 BOOST_GPU_ENABLED
130 operator T&()
131 {
132 return data_;
133 }
134
135} ;
136
137template<class T>
138BOOST_GPU_ENABLED
139T const& get ( initialized<T> const& x )
140{
141 return x.data() ;
142}
143
144template<class T>
145BOOST_GPU_ENABLED
146T& get ( initialized<T>& x )
147{
148 return x.data() ;
149}
150
151template<class T>
152BOOST_GPU_ENABLED
153void swap ( initialized<T> & lhs, initialized<T> & rhs )
154{
155 lhs.swap(rhs) ;
156}
157
158template<class T>
159class value_initialized
160{
161 private :
162
163 // initialized<T> does value-initialization by default.
164 initialized<T> m_data;
165
166 public :
167
168 BOOST_GPU_ENABLED
169 value_initialized()
170 :
171 m_data()
172 { }
173
174 BOOST_GPU_ENABLED
175 T const & data() const
176 {
177 return m_data.data();
178 }
179
180 BOOST_GPU_ENABLED
181 T& data()
182 {
183 return m_data.data();
184 }
185
186 BOOST_GPU_ENABLED
187 void swap(value_initialized & arg)
188 {
189 m_data.swap(arg.m_data);
190 }
191
192 BOOST_GPU_ENABLED
193 operator T const &() const
194 {
195 return m_data;
196 }
197
198 BOOST_GPU_ENABLED
199 operator T&()
200 {
201 return m_data;
202 }
203} ;
204
205
206template<class T>
207BOOST_GPU_ENABLED
208T const& get ( value_initialized<T> const& x )
209{
210 return x.data() ;
211}
212
213template<class T>
214BOOST_GPU_ENABLED
215T& get ( value_initialized<T>& x )
216{
217 return x.data() ;
218}
219
220template<class T>
221BOOST_GPU_ENABLED
222void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs )
223{
224 lhs.swap(rhs) ;
225}
226
227
228class initialized_value_t
229{
230 public :
231
232 template <class T> BOOST_GPU_ENABLED operator T() const
233 {
234 return initialized<T>().data();
235 }
236};
237
238initialized_value_t const initialized_value = {} ;
239
240
241} // namespace boost
242
243#ifdef BOOST_MSVC
244#pragma warning(pop)
245#endif
246
247#endif
248

source code of include/boost/utility/value_init.hpp