forked from boostorg/utility
Compare commits
46 Commits
boost-1.35
...
svn-branch
Author | SHA1 | Date | |
---|---|---|---|
|
e2308ce3c9 | ||
|
c733d07560 | ||
|
d497ff80e7 | ||
|
bb3fffd929 | ||
|
928091779d | ||
|
fc960ec5ef | ||
|
e76c779b8c | ||
|
1a4aa43cad | ||
|
9ece1d1d4c | ||
|
12de918a3a | ||
|
882d38c2c7 | ||
|
33041ad664 | ||
|
6a2aa822f8 | ||
|
09ab16bfc1 | ||
|
ec46e40809 | ||
|
b3a971e7e9 | ||
|
7ddb559887 | ||
|
ea8c99b1d5 | ||
|
56b0846099 | ||
|
42e0001370 | ||
|
cd8f85afee | ||
|
bddd52c4b9 | ||
|
8f03aeac4e | ||
|
3bb2568fad | ||
|
01e91a3799 | ||
|
55f3c351a3 | ||
|
3f72b10182 | ||
|
71cb8cb574 | ||
|
c950825ef4 | ||
|
66ca84a45d | ||
|
06404f7d39 | ||
|
2d860e2574 | ||
|
66514f61ff | ||
|
63cde4d3fd | ||
|
1950f292df | ||
|
92a0602190 | ||
|
c9a3ab1d04 | ||
|
0782034333 | ||
|
0808883f3c | ||
|
2f69501e55 | ||
|
5b83f641a8 | ||
|
c730ab4ffb | ||
|
e55610a0d0 | ||
|
bf968794c9 | ||
|
ce6e9c6698 | ||
|
7ac180ed54 |
@@ -740,16 +740,26 @@ specialisation).</p>
|
||||
|
||||
<p>Revised 01 September 2000</p>
|
||||
|
||||
<p>
|
||||
Copyright 2000 Steve Cleary, Beman Dawes, Howard
|
||||
Hinnant and John Maddock. <br/>
|
||||
Use, modification and distribution are subject to the
|
||||
Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt
|
||||
or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">
|
||||
http://www.boost.org/LICENSE_1_0.txt
|
||||
</a>).
|
||||
</p>
|
||||
<p><EFBFBD> Copyright boost.org 2000. Permission to copy, use, modify,
|
||||
sell and distribute this document is granted provided this
|
||||
copyright notice appears in all copies. This document is provided
|
||||
"as is" without express or implied warranty, and with
|
||||
no claim as to its suitability for any purpose.</p>
|
||||
|
||||
<p>Based on contributions by Steve Cleary, Beman Dawes, Howard
|
||||
Hinnant and John Maddock.</p>
|
||||
|
||||
<p>Maintained by <a href="mailto:john@johnmaddock.co.uk">John
|
||||
Maddock</a>, the latest version of this file can be found at <a
|
||||
href="http://www.boost.org/">www.boost.org</a>, and the boost
|
||||
discussion list at <a
|
||||
href="http://www.yahoogroups.com/list/boost">www.yahoogroups.com/list/boost</a>.</p>
|
||||
|
||||
<p>.</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p> </p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
@@ -366,18 +366,17 @@ David Vandevoorde and Nicolai M. Josuttis.
|
||||
<EM>C++ Templates: The Complete Guide</EM>.
|
||||
Addison-Wesley, 2002.</DL>
|
||||
|
||||
<hr/>
|
||||
<p>Copyright Jaakko Järvi, Jeremiah Willcock and Andrew Lumsdaine<BR>
|
||||
|
||||
|
||||
|
||||
|
||||
<hr></hr>
|
||||
|
||||
<B>Contributed by:</B> <BR>
|
||||
Jaakko Järvi, Jeremiah Willcock and Andrew Lumsdaine<BR>
|
||||
<EM>{jajarvi|jewillco|lums}@osl.iu.edu</EM><BR>
|
||||
Indiana University<BR>
|
||||
Open Systems Lab<br/>
|
||||
Use, modification and distribution are subject to the
|
||||
Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt
|
||||
or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">
|
||||
http://www.boost.org/LICENSE_1_0.txt
|
||||
</a>).
|
||||
</p>
|
||||
Open Systems Lab
|
||||
<!--HTMLFOOT-->
|
||||
<!--ENDHTML-->
|
||||
<!--FOOTER-->
|
||||
|
@@ -10,9 +10,10 @@
|
||||
#define BOOST_RESULT_OF_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/type_traits/ice.hpp>
|
||||
#include <boost/type.hpp>
|
||||
#include <boost/preprocessor.hpp>
|
||||
#include <boost/preprocessor/iteration/iterate.hpp>
|
||||
#include <boost/preprocessor/punctuation/comma_if.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_shifted_params.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/mpl/has_xxx.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
|
@@ -1,31 +1,20 @@
|
||||
// (C) Copyright 2002-2007, Fernando Luis Cacciola Carballal.
|
||||
// (C) Copyright 2002-2008, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// 21 Ago 2002 (Created) Fernando Cacciola
|
||||
// 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker
|
||||
// 30 Jan 2008 (Worked around compiler bugs, added initialized_value) Fernando Cacciola, Niels Dekker
|
||||
//
|
||||
#ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
||||
#define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
||||
|
||||
// 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
|
||||
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744
|
||||
// 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.
|
||||
// fact that various compilers haven't fully implemented value-initialization.
|
||||
// The constructor of boost::value_initialized<T> works around these compiler
|
||||
// issues, by clearing the bytes of T, before constructing the T object it
|
||||
// contains. More details on these issues are at libs/utility/value_init.htm
|
||||
|
||||
#include <boost/aligned_storage.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
@@ -48,23 +37,32 @@ class value_initialized
|
||||
remove_const<T>::type data;
|
||||
};
|
||||
|
||||
mutable aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value> x;
|
||||
mutable
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
|
||||
typename
|
||||
#endif
|
||||
aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x;
|
||||
|
||||
wrapper * wrapper_address() const
|
||||
{
|
||||
return static_cast<wrapper *>( static_cast<void*>(&x));
|
||||
}
|
||||
|
||||
public :
|
||||
|
||||
value_initialized()
|
||||
{
|
||||
std::memset(x.address(), 0, sizeof(x));
|
||||
std::memset(&x, 0, sizeof(x));
|
||||
#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.
|
||||
// will be default-initialized". It is safe to ignore this warning when using value_initialized.
|
||||
#pragma warning(disable: 4345)
|
||||
#endif
|
||||
#endif
|
||||
new (x.address()) wrapper();
|
||||
new (wrapper_address()) wrapper();
|
||||
#ifdef BOOST_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
@@ -72,25 +70,23 @@ class value_initialized
|
||||
|
||||
value_initialized(value_initialized const & arg)
|
||||
{
|
||||
new (x.address()) wrapper( *static_cast<wrapper const *>(arg.x.address()) );
|
||||
new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
|
||||
}
|
||||
|
||||
value_initialized & operator=(value_initialized const & arg)
|
||||
{
|
||||
T & this_data = this->data();
|
||||
T const & arg_data = arg.data();
|
||||
this_data = arg_data;
|
||||
this->data() = static_cast<T const &>( arg.data() );
|
||||
return *this;
|
||||
}
|
||||
|
||||
~value_initialized()
|
||||
{
|
||||
static_cast<wrapper *>(x.address())->wrapper::~wrapper();
|
||||
wrapper_address()->wrapper::~wrapper();
|
||||
}
|
||||
|
||||
T& data() const
|
||||
{
|
||||
return static_cast<wrapper *>(x.address())->data;
|
||||
return wrapper_address()->data;
|
||||
}
|
||||
|
||||
operator T&() const { return this->data(); }
|
||||
@@ -110,6 +106,18 @@ T& get ( value_initialized<T>& x )
|
||||
return x.data() ;
|
||||
}
|
||||
|
||||
|
||||
class initialized_value
|
||||
{
|
||||
public :
|
||||
|
||||
template <class T> operator T() const
|
||||
{
|
||||
return get( value_initialized<T>() );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace boost
|
||||
|
||||
|
||||
|
@@ -22,14 +22,13 @@
|
||||
<a href="current_function.html">current_function</a><br>
|
||||
<a href="enable_if.html">enable_if</a><br>
|
||||
<a href="iterator_adaptors.htm">iterator_adaptors</a><br>
|
||||
<a href="generator_iterator.htm">generator iterator adaptors</a><br>
|
||||
<a href="operators.htm">operators</a><br>
|
||||
<a href="throw_exception.html">throw_exception</a><br>
|
||||
<a href="utility.htm">utility</a><br>
|
||||
<a href="value_init.htm">value_init</a></p>
|
||||
</blockquote>
|
||||
<hr>
|
||||
<p>© Copyright Beman Dawes, 2001</p>
|
||||
<p><EFBFBD> Copyright Beman Dawes, 2001</p>
|
||||
<p>Distributed under the Boost Software License, Version 1.0. (See
|
||||
accompanying file <a href="../../LICENSE_1_0.txt">
|
||||
LICENSE_1_0.txt</a> or copy at
|
||||
|
@@ -315,7 +315,7 @@ Last modified: Mon Aug 11 11:27:03 EST 2003
|
||||
<p><EFBFBD> Copyright 2003 The Trustees of Indiana University.
|
||||
Use, modification and distribution is subject to 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)</p>
|
||||
http://www.boost.org/LICENSE_1_0.txt)</p>
|
||||
|
||||
</body>
|
||||
|
||||
|
0
test/next_prior_test.cpp
Normal file → Executable file
0
test/next_prior_test.cpp
Normal file → Executable file
@@ -32,7 +32,8 @@
|
||||
</dl>
|
||||
|
||||
<ul>
|
||||
<li><a href="#val_init"><code>value_initialized<T></code></a></li>
|
||||
<li><a href="#val_init"><code>template class value_initialized<T></code></a></li>
|
||||
<li><a href="#initialized_value"><code>class initialized_value</code></a></li>
|
||||
|
||||
</ul>
|
||||
<a href="#acknowledgements">Acknowledgements</a><br>
|
||||
@@ -52,6 +53,9 @@ union and class types.
|
||||
Moreover, <code>value_initialized</code> offers a workaround to various
|
||||
compiler issues regarding value-initialization.
|
||||
|
||||
Furthermore a convenience class, <code>initialized_value</code> is provided,
|
||||
to avoid repeating the type name when retrieving the value from a
|
||||
<code>value_initialized<T></code> object.
|
||||
<br>
|
||||
</p>
|
||||
|
||||
@@ -117,6 +121,16 @@ constructed by the following declaration:
|
||||
<pre>
|
||||
value_initialized<T> var;
|
||||
</pre>
|
||||
</p>
|
||||
<p>
|
||||
The convenience class <a href="#initialized_value"><code>initialized_value</code></a>
|
||||
allows value-initializing a variable as follows:
|
||||
<pre>
|
||||
T var = initialized_value();
|
||||
</pre>
|
||||
This form of initialization is also very similar to <code>T4 var4 = T4()</code>,
|
||||
but robust against the aforementioned compiler issues.
|
||||
|
||||
</p>
|
||||
|
||||
<h2><a name="details"></a>Details</h2>
|
||||
@@ -298,6 +312,40 @@ the wrapped object is always performed with the <code>get()</code> idiom:</p>
|
||||
|
||||
<pre>value_initialized<int> x ;<br>get(x) = 1 ; // OK<br><br>value_initialized<int const> cx ;<br>get(x) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized<int> const x_c ;<br>get(x_c) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized<int const> const cx_c ;<br>get(cx_c) = 1 ; // ERROR: Cannot modify a const object<br></pre>
|
||||
|
||||
<h2><a name="initialized_value"><code>class initialized_value</code></a></h2>
|
||||
|
||||
<pre>
|
||||
namespace boost {
|
||||
class initialized_value
|
||||
{
|
||||
public :
|
||||
template <class T> operator T() const ;
|
||||
};
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
The class <code>initialized_value</code> provides a convenient way to get
|
||||
an initialized value: its conversion operator provides an appropriate
|
||||
<em>value-initialized</em> object for any CopyConstructible type.
|
||||
|
||||
Suppose you need to have an initialized variable of type <code>T</code>.
|
||||
You could do it as follows:
|
||||
<pre>
|
||||
T var = T();
|
||||
</pre>
|
||||
But as mentioned before, this form suffers from various compiler issues.
|
||||
The template <code>value_initialized</code> offers a workaround:
|
||||
<pre>
|
||||
T var = get( value_initialized<T>() );
|
||||
</pre>
|
||||
Unfortunately both forms repeat the type name, which
|
||||
is rather short now (<code>T</code>), but could of course be
|
||||
more like <code>Namespace::Template<Arg>::Type</code>.
|
||||
Instead, one could use <code>initialized_value</code> as follows:
|
||||
<pre>
|
||||
T var = initialized_value();
|
||||
</pre>
|
||||
|
||||
<h3><a name="references">References</a></h3>
|
||||
[1] Bjarne Stroustrup, Gabriel Dos Reis, and J. Stephen Adamczyk wrote
|
||||
various papers, proposing to extend the support for brace-enclosed <em>initializer lists</em>
|
||||
@@ -326,7 +374,7 @@ for Boost release version 1.35 (2008), offering a workaround to various compiler
|
||||
</p>
|
||||
|
||||
<hr>
|
||||
<p>Revised 15 January 2008</p>
|
||||
<p>Revised 16 January 2008</p>
|
||||
|
||||
<p>© Copyright Fernando Cacciola, 2002, 2008.</p>
|
||||
|
||||
|
@@ -6,8 +6,8 @@
|
||||
//
|
||||
// Test program for "boost/utility/value_init.hpp"
|
||||
//
|
||||
// 21 Agu 2002 (Created) Fernando Cacciola
|
||||
// 15 Jan 2008 (Added tests regarding compiler issues) Fernando Cacciola, Niels Dekker
|
||||
// 21 Ago 2002 (Created) Fernando Cacciola
|
||||
// 19 Jan 2008 (Added tests regarding compiler issues and initialized_value) Fernando Cacciola, Niels Dekker
|
||||
|
||||
#include <cstring> // For memcmp.
|
||||
#include <iostream>
|
||||
@@ -52,7 +52,7 @@ struct NonPODBase
|
||||
struct NonPOD : NonPODBase
|
||||
{
|
||||
NonPOD () : id() {}
|
||||
NonPOD ( std::string const& id_) : id(id_) {}
|
||||
explicit NonPOD ( std::string const& id_) : id(id_) {}
|
||||
|
||||
friend std::ostream& operator << ( std::ostream& os, NonPOD const& npod )
|
||||
{ return os << '(' << npod.id << ')' ; }
|
||||
@@ -180,6 +180,32 @@ struct CopyFunctionCallTester
|
||||
};
|
||||
|
||||
|
||||
template<class T>
|
||||
void check_initialized_value ( T const& y )
|
||||
{
|
||||
T initializedValue = boost::initialized_value() ;
|
||||
BOOST_CHECK ( y == initializedValue ) ;
|
||||
}
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#if __BORLANDC__ == 0x582
|
||||
void check_initialized_value( NonPOD const& )
|
||||
{
|
||||
// The initialized_value check is skipped for Borland 5.82
|
||||
// and this type (NonPOD), because the following statement
|
||||
// won't compile on this particular compiler version:
|
||||
// NonPOD initializedValue = boost::initialized_value() ;
|
||||
//
|
||||
// This is caused by a compiler bug, that is fixed with a newer version
|
||||
// of the Borland compiler. The Release Notes for Delphi(R) 2007 for
|
||||
// Win32(R) and C++Builder(R) 2007 (http://dn.codegear.com/article/36575)
|
||||
// say about similar statements:
|
||||
// both of these statements now compile but under 5.82 got the error:
|
||||
// Error E2015: Ambiguity between 'V::V(const A &)' and 'V::V(const V &)'
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//
|
||||
// This test function tests boost::value_initialized<T> for a specific type T.
|
||||
// The first argument (y) is assumed have the value of a value-initialized object.
|
||||
@@ -189,9 +215,13 @@ template<class T>
|
||||
bool test ( T const& y, T const& z )
|
||||
{
|
||||
const boost::unit_test::counter_t counter_before_test = boost::minimal_test::errors_counter();
|
||||
|
||||
check_initialized_value(y);
|
||||
|
||||
boost::value_initialized<T> x ;
|
||||
BOOST_CHECK ( y == x ) ;
|
||||
BOOST_CHECK ( y == boost::get(x) ) ;
|
||||
|
||||
static_cast<T&>(x) = z ;
|
||||
boost::get(x) = z ;
|
||||
BOOST_CHECK ( x == z ) ;
|
||||
|
Reference in New Issue
Block a user