forked from boostorg/utility
Compare commits
26 Commits
svn-branch
...
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 |
@ -145,14 +145,20 @@ objects are is implemented in <a href="../../boost/utility/compare_pointees.hpp"
|
||||
so direct usage of relational operators with the implied aliasing of shallow semantics
|
||||
-as with pointers- should not be used with generic code written for this concept.</p>
|
||||
|
||||
<h3>Acknowledgements</h3>
|
||||
<p>Based on the original concept developed by Augustus Saunders.
|
||||
|
||||
<br>
|
||||
</p>
|
||||
<HR>
|
||||
<TABLE>
|
||||
<TR valign=top>
|
||||
<TD nowrap>Copyright © 2003</TD><TD>
|
||||
<A HREF="mailto:fernando_cacciola@hotmail.com">Fernando Cacciola</A>,
|
||||
based on the original concept developed by Augustus Saunders.
|
||||
<A HREF="mailto:fernando_cacciola@hotmail.com">Fernando Cacciola</A>
|
||||
</TD></TR></TABLE>
|
||||
|
||||
<p>Distributed under the Boost Software License, Version 1.0. See
|
||||
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
|
||||
|
||||
</BODY>
|
||||
</HTML>
|
12
assert.html
12
assert.html
@ -47,9 +47,15 @@ void assertion_failed(char const * expr, char const * function, char const * fil
|
||||
<P>As is the case with <STRONG><cassert></STRONG>, <STRONG><boost/assert.hpp></STRONG>
|
||||
can be included multiple times in a single translation unit. <STRONG>BOOST_ASSERT</STRONG>
|
||||
will be redefined each time as specified above.</P>
|
||||
<p><STRONG><boost/assert.hpp></STRONG> also defines the macro <STRONG>BOOST_VERIFY</STRONG>.
|
||||
It has exactly the same behavior as <STRONG>BOOST_ASSERT</STRONG>, except that
|
||||
the expression that is passed to <STRONG>BOOST_VERIFY</STRONG> is always
|
||||
evaluated. This is useful when the asserted expression has desirable side
|
||||
effects; it can also help suppress warnings about unused variables when the
|
||||
only use of the variable is inside an assertion.</p>
|
||||
<p><br>
|
||||
<small>Copyright <20> 2002 by Peter Dimov. 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 <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>.</small></p>
|
||||
<small>Copyright <20> 2002, 2007 by Peter Dimov. 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 <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>.</small></p>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -334,7 +334,7 @@ with the exact pointer type used in <code>switcher</code>'s constructor.</p>
|
||||
<h3><a name="contributors">Contributors</a></h3>
|
||||
|
||||
<dl>
|
||||
<dt><a href="../../people/ed_brey.htm">Ed Brey</a>
|
||||
<dt><a href="http://www.boost.org/people/ed_brey.htm">Ed Brey</a>
|
||||
<dd>Suggested some interface changes.
|
||||
|
||||
<dt><a href="http://www.moocat.org">R. Samuel Klatchko</a> (<a
|
||||
@ -343,7 +343,7 @@ with the exact pointer type used in <code>switcher</code>'s constructor.</p>
|
||||
<dd>Invented the idiom of how to use a class member for initializing
|
||||
a base class.
|
||||
|
||||
<dt><a href="../../people/dietmar_kuehl.htm">Dietmar Kuehl</a>
|
||||
<dt><a href="http://www.boost.org/people/dietmar_kuehl.htm">Dietmar Kuehl</a>
|
||||
<dd>Popularized the base-from-member idiom in his
|
||||
<a href="http://www.informatik.uni-konstanz.de/~kuehl/c++/iostream/">IOStream
|
||||
example classes</a>.
|
||||
@ -353,7 +353,7 @@ with the exact pointer type used in <code>switcher</code>'s constructor.</p>
|
||||
can be controlled and automated with macros. The implementation uses
|
||||
the <a href="../preprocessor/index.html">Preprocessor library</a>.
|
||||
|
||||
<dt><a href="../../people/daryle_walker.html">Daryle Walker</a>
|
||||
<dt><a href="http://www.boost.org/people/daryle_walker.html">Daryle Walker</a>
|
||||
<dd>Started the library. Contributed the test file <cite><a
|
||||
href="base_from_member_test.cpp">base_from_member_test.cpp</a></cite>.
|
||||
</dl>
|
||||
|
@ -3,7 +3,7 @@
|
||||
<title>Header </title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="Template" content="C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE\html.dot">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
|
||||
<boostcompressed_pair.hpp>
|
||||
</head>
|
||||
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080">
|
||||
@ -59,17 +59,18 @@ public:
|
||||
empty type, then assigning to that member will produce memory corruption,
|
||||
unless the empty type has a "do nothing" assignment operator defined. This is
|
||||
due to a bug in the way VC6 generates implicit assignment operators.</p>
|
||||
<hr>
|
||||
<p>Revised 08 May 2001</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>
|
||||
<h3>Acknowledgements</h3>
|
||||
<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>
|
||||
<hr>
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan -->07 November 2007<!--webbot bot="Timestamp" endspan i-checksum="40338" --></p>
|
||||
<p><EFBFBD> Copyright Beman Dawes, 2000.</p>
|
||||
<p>Distributed under the Boost Software License, Version 1.0. See
|
||||
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
@ -153,7 +153,7 @@ int main()
|
||||
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->05 December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38516" --></p>
|
||||
|
||||
<p><i>Copyright © 2001 <a href=
|
||||
"../../people/jens_maurer.htm">Jens Maurer</a></i></p>
|
||||
"http://www.boost.org/people/jens_maurer.htm">Jens Maurer</a></i></p>
|
||||
|
||||
<p><i>Distributed under the Boost Software License, Version 1.0. (See
|
||||
accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
|
||||
|
@ -2,6 +2,7 @@
|
||||
// boost/assert.hpp - BOOST_ASSERT(expr)
|
||||
//
|
||||
// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
|
||||
// Copyright (c) 2007 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
@ -35,3 +36,15 @@ void assertion_failed(char const * expr, char const * function, char const * fil
|
||||
# include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same
|
||||
# define BOOST_ASSERT(expr) assert(expr)
|
||||
#endif
|
||||
|
||||
#undef BOOST_VERIFY
|
||||
|
||||
#if defined(BOOST_DISABLE_ASSERTS) || ( !defined(BOOST_ENABLE_ASSERT_HANDLER) && defined(NDEBUG) )
|
||||
|
||||
# define BOOST_VERIFY(expr) ((void)(expr))
|
||||
|
||||
#else
|
||||
|
||||
# define BOOST_VERIFY(expr) BOOST_ASSERT(expr)
|
||||
|
||||
#endif
|
||||
|
@ -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>
|
||||
|
@ -54,8 +54,8 @@ public:
|
||||
|
||||
void* apply (void* address, std::size_t n) const
|
||||
{
|
||||
for(char* next = address = this->apply(address); !! --n;)
|
||||
this->apply(next = next+sizeof(T));
|
||||
for(void* next = address = this->apply(address); !! --n;)
|
||||
this->apply(next = static_cast<char *>(next) + sizeof(T));
|
||||
return address;
|
||||
}
|
||||
|
||||
|
@ -1,68 +1,100 @@
|
||||
// (C) 2002, 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
|
||||
// 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
|
||||
|
||||
#include <boost/detail/select_type.hpp>
|
||||
#include <boost/type_traits/cv_traits.hpp>
|
||||
// Note: The implementation of boost::value_initialized had to deal with the
|
||||
// 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>
|
||||
#include <boost/type_traits/cv_traits.hpp>
|
||||
#include <boost/type_traits/alignment_of.hpp>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace vinit_detail {
|
||||
|
||||
template<class T>
|
||||
class const_T_base
|
||||
class value_initialized
|
||||
{
|
||||
protected :
|
||||
|
||||
const_T_base() : x() {}
|
||||
|
||||
T x ;
|
||||
} ;
|
||||
|
||||
template<class T>
|
||||
struct non_const_T_base
|
||||
{
|
||||
protected :
|
||||
|
||||
non_const_T_base() : x() {}
|
||||
|
||||
mutable T x ;
|
||||
} ;
|
||||
|
||||
template<class T>
|
||||
struct select_base
|
||||
{
|
||||
typedef
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
|
||||
typename
|
||||
private :
|
||||
struct wrapper
|
||||
{
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
|
||||
typename
|
||||
#endif
|
||||
::boost::detail::if_true< ::boost::is_const<T>::value >
|
||||
::template then< const_T_base<T>, non_const_T_base<T> >::type type ;
|
||||
} ;
|
||||
remove_const<T>::type data;
|
||||
};
|
||||
|
||||
} // namespace vinit_detail
|
||||
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));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
class value_initialized : private vinit_detail::select_base<T>::type
|
||||
{
|
||||
public :
|
||||
|
||||
value_initialized() {}
|
||||
value_initialized()
|
||||
{
|
||||
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". It is safe to ignore this warning when using value_initialized.
|
||||
#pragma warning(disable: 4345)
|
||||
#endif
|
||||
#endif
|
||||
new (wrapper_address()) wrapper();
|
||||
#ifdef BOOST_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
}
|
||||
|
||||
operator T&() const { return this->x ; }
|
||||
value_initialized(value_initialized const & arg)
|
||||
{
|
||||
new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
|
||||
}
|
||||
|
||||
T& data() const { return this->x ; }
|
||||
value_initialized & operator=(value_initialized const & arg)
|
||||
{
|
||||
this->data() = static_cast<T const &>( arg.data() );
|
||||
return *this;
|
||||
}
|
||||
|
||||
~value_initialized()
|
||||
{
|
||||
wrapper_address()->wrapper::~wrapper();
|
||||
}
|
||||
|
||||
T& data() const
|
||||
{
|
||||
return wrapper_address()->data;
|
||||
}
|
||||
|
||||
operator T&() const { return this->data(); }
|
||||
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
template<class T>
|
||||
T const& get ( value_initialized<T> const& x )
|
||||
{
|
||||
@ -74,8 +106,19 @@ 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
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -2029,13 +2029,13 @@ public:
|
||||
<h2><a name="contributors">Contributors</a></h2>
|
||||
|
||||
<dl>
|
||||
<dt><a href="../../people/dave_abrahams.htm">Dave Abrahams</a></dt>
|
||||
<dt><a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a></dt>
|
||||
|
||||
<dd>Started the library and contributed the arithmetic operators in
|
||||
<cite><a href=
|
||||
"../../boost/operators.hpp">boost/operators.hpp</a></cite>.</dd>
|
||||
|
||||
<dt><a href="../../people/jeremy_siek.htm">Jeremy Siek</a></dt>
|
||||
<dt><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</a></dt>
|
||||
|
||||
<dd>Contributed the <a href="#deref">dereference operators and iterator
|
||||
helpers</a> in <cite><a href=
|
||||
@ -2043,19 +2043,19 @@ public:
|
||||
contributed <cite><a href=
|
||||
"iterators_test.cpp">iterators_test.cpp</a></cite>.</dd>
|
||||
|
||||
<dt><a href="../../people/aleksey_gurtovoy.htm">Aleksey
|
||||
<dt><a href="http://www.boost.org/people/aleksey_gurtovoy.htm">Aleksey
|
||||
Gurtovoy</a></dt>
|
||||
|
||||
<dd>Contributed the code to support <a href="#chaining">base class
|
||||
chaining</a> while remaining backward-compatible with old versions of
|
||||
the library.</dd>
|
||||
|
||||
<dt><a href="../../people/beman_dawes.html">Beman Dawes</a></dt>
|
||||
<dt><a href="http://www.boost.org/people/beman_dawes.html">Beman Dawes</a></dt>
|
||||
|
||||
<dd>Contributed <cite><a href=
|
||||
"operators_test.cpp">operators_test.cpp</a></cite>.</dd>
|
||||
|
||||
<dt><a href="../../people/daryle_walker.html">Daryle Walker</a></dt>
|
||||
<dt><a href="http://www.boost.org/people/daryle_walker.html">Daryle Walker</a></dt>
|
||||
|
||||
<dd>Contributed classes for the shift operators, equivalence, partial
|
||||
ordering, and arithmetic conversions. Added the grouped operator
|
||||
|
@ -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>
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
# Copyright David Abrahams 2003. Permission to copy, use,
|
||||
# modify, sell and distribute this software is granted provided this
|
||||
# copyright notice appears in all copies. This software is provided
|
||||
# "as is" without express or implied warranty, and with no claim as
|
||||
# to its suitability for any purpose.
|
||||
# Copyright David Abrahams 2003.
|
||||
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# See http://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
# For more information, see http://www.boost.org/
|
||||
|
||||
@ -33,4 +32,6 @@ test-suite utility
|
||||
[ compile-fail ../value_init_test_fail1.cpp ]
|
||||
[ compile-fail ../value_init_test_fail2.cpp ]
|
||||
[ compile-fail ../value_init_test_fail3.cpp ]
|
||||
[ run ../verify_test.cpp ]
|
||||
;
|
||||
|
||||
|
16
utility.htm
16
utility.htm
@ -68,7 +68,7 @@ const std::list<T>::iterator next = boost::next(prev, 2);</pre>
|
||||
<p>The distance from the given iterator should be supplied as an absolute value. For
|
||||
example, the iterator four iterators prior to the given iterator <code>p</code>
|
||||
may be obtained by <code>prior(p, 4)</code>.</p>
|
||||
<p>Contributed by <a href="../../people/dave_abrahams.htm">Dave Abrahams</a>. Two-argument versions by Daniel Walker.</p>
|
||||
<p>Contributed by <a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>. Two-argument versions by Daniel Walker.</p>
|
||||
<h2><a name="Class_noncopyable">Class noncopyable</a></h2>
|
||||
<p>Class <strong>noncopyable</strong> is a base class. Derive your own class
|
||||
from <strong>noncopyable</strong> when you want to prohibit copy construction
|
||||
@ -89,7 +89,7 @@ const std::list<T>::iterator next = boost::next(prev, 2);</pre>
|
||||
to verify class <b>noncopyable</b> works as expected. It has have been run
|
||||
successfully under GCC 2.95, Metrowerks CodeWarrior 5.0, and Microsoft Visual
|
||||
C++ 6.0 sp 3.</p>
|
||||
<p>Contributed by <a href="../../people/dave_abrahams.htm">Dave Abrahams</a>.</p>
|
||||
<p>Contributed by <a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>.</p>
|
||||
<h3>Example</h3>
|
||||
<blockquote>
|
||||
<pre>// inside one of your own headers ...
|
||||
@ -183,12 +183,12 @@ void f() {
|
||||
<p>See <a href="base_from_member.html">separate documentation</a>.</p>
|
||||
<hr>
|
||||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan
|
||||
-->02 May, 2004<!--webbot bot="Timestamp" endspan i-checksum="38582"
|
||||
-->07 November, 2007<!--webbot bot="Timestamp" endspan i-checksum="39369"
|
||||
-->
|
||||
</p>
|
||||
<p>© Copyright boost.org 1999-2003. 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>© Copyright Beman Dawes 1999-2003.</p>
|
||||
<p>Distributed under the Boost Software License, Version 1.0. See
|
||||
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
229
value_init.htm
229
value_init.htm
@ -17,11 +17,13 @@
|
||||
<dl>
|
||||
<dt><a href="#rationale">Rationale</a></dt>
|
||||
<dt><a href="#intro">Introduction</a></dt>
|
||||
<dt><a href="#details">Details</a></dt>
|
||||
</dl>
|
||||
|
||||
<ul>
|
||||
<li><a href="#valueinit">value-initialization</a></li>
|
||||
<li><a href="#valueinitsyn">value-initialization syntax</a></li>
|
||||
<li><a href="#compiler_issues">compiler issues</a></li>
|
||||
|
||||
</ul>
|
||||
|
||||
@ -30,7 +32,8 @@
|
||||
</dl>
|
||||
|
||||
<ul>
|
||||
<li><a href="#val_init"><code>value_initialized<></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>
|
||||
@ -44,24 +47,104 @@
|
||||
for initialization. Depending on the type, the value of a newly constructed
|
||||
object can be zero-initialized (logically 0), default-constructed (using
|
||||
the default constructor), or indeterminate. When writing generic code,
|
||||
this problem must be addressed. <code>value_initialized</code> provides
|
||||
this problem must be addressed. The template <code>value_initialized</code> provides
|
||||
a solution with consistent syntax for value initialization of scalar,
|
||||
union and class types. <br>
|
||||
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>
|
||||
|
||||
<h2><a name="intro"></a>Introduction</h2>
|
||||
|
||||
<p>The C++ standard [<a href="#references">1</a>] contains the definitions
|
||||
<p>
|
||||
There are various ways to initialize a variable, in C++. The following
|
||||
declarations all <em>may</em> have a local variable initialized to its default
|
||||
value:
|
||||
<pre>
|
||||
T1 var1;
|
||||
T2 var2 = 0;
|
||||
T3 var3 = {};
|
||||
T4 var4 = T4();
|
||||
</pre>
|
||||
Unfortunately, whether or not any of those declarations correctly
|
||||
initialize the variable very much depends on its type. The first
|
||||
declaration is valid for any <a href="http://www.sgi.com/tech/stl/DefaultConstructible.html">
|
||||
DefaultConstructible</a> type (by definition).
|
||||
However, it does not always do an initialization!
|
||||
It correctly initializes the variable when it's an instance of a
|
||||
class, and the author of the class has provided a proper default
|
||||
constructor. On the other hand, the value of <code>var1</code> is <em>indeterminate</em> when
|
||||
its type is an arithmetic type, like <code>int</code>, <code>float</code>, or <code>char</code>.
|
||||
An arithmetic variable
|
||||
is of course initialized properly by the second declaration, <code>T2
|
||||
var2 = 0</code>. But this initialization form usually won't work for a
|
||||
class type (unless the class was especially written to support being
|
||||
initialized that way). The third form, <code>T3 var3 = {}</code>
|
||||
initializes an aggregate, typically a "C-style" <code>struct</code> or a "C-style" array.
|
||||
However, the syntax is not allowed for a class that has an explicitly declared
|
||||
constructor. (But watch out for an upcoming C++ language change,
|
||||
by Bjarne Stroustrup et al [<a href="#references">1</a>]!)
|
||||
The fourth form is the most generic form of them, as it
|
||||
can be used to initialize arithmetic types, class types, aggregates, pointers, and
|
||||
other types. The declaration, <code>T4 var4 = T4()</code>, should be read
|
||||
as follows: First a temporary object is created, by <code>T4()</code>.
|
||||
This object is <a href="#valueinit">value-initialized</a>. Next the temporary
|
||||
object is copied to the named variable, <code>var4</code>. Afterwards, the temporary
|
||||
is destroyed. While the copying and the destruction are likely to
|
||||
be optimized away, C++ still requires the type <code>T4</code> to be
|
||||
<a href="CopyConstructible.html">CopyConstructible</a>.
|
||||
(So <code>T4</code> needs to be <em>both</em> DefaultConstructible <em>and</em> CopyConstructible.)
|
||||
A class may not be CopyConstructible, for example because it may have a
|
||||
private and undefined copy constructor,
|
||||
or because it may be derived from <a href="utility.htm#Class_noncopyable">boost::noncopyable</a>.
|
||||
Scott Meyers [<a href="#references">2</a>] explains why a class would be defined like that.
|
||||
</p>
|
||||
<p>
|
||||
There is another, less obvious disadvantage to the fourth form, <code>T4 var4 = T4()</code>:
|
||||
It suffers from various <a href="#compiler_issues">compiler issues</a>, causing
|
||||
a variable to be left uninitialized in some compiler specific cases.
|
||||
</p>
|
||||
<p>
|
||||
The template <a href="#val_init"><code>value_initialized</code></a>
|
||||
offers a generic way to initialize
|
||||
an object, like <code>T4 var4 = T4()</code>, but without requiring its type
|
||||
to be CopyConstructible. And it offers a workaround to those compiler issues
|
||||
regarding value-initialization as well! It allows getting an initialized
|
||||
variable of any type; it <em>only</em> requires the type to be DefaultConstructible.
|
||||
A properly <em>value-initialized</em> object of type <code>T</code> is
|
||||
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>
|
||||
<p>The C++ standard [<a href="#references">3</a>] contains the definitions
|
||||
of <code>zero-initialization</code> and <code>default-initialization</code>.
|
||||
Informally, zero-initialization means that the object is given the initial
|
||||
value 0 (converted to the type) and default-initialization means that
|
||||
POD [<a href="#references">2</a>] types are zero-initialized, while class
|
||||
POD [<a href="#references">4</a>] types are zero-initialized, while non-POD class
|
||||
types are initialized with their corresponding default constructors. A
|
||||
<i>declaration</i> can contain an <i>initializer</i>, which specifies the
|
||||
object's initial value. The initializer can be just '()', which states that
|
||||
the object shall be default-initialized (but see below). However, if a <i>declaration</i>
|
||||
the object shall be value-initialized (but see below). However, if a <i>declaration</i>
|
||||
has no <i>initializer</i> and it is of a non-<code>const</code>, non-<code>static</code>
|
||||
POD type, the initial value is indeterminate:<cite>(see §8.5 for the
|
||||
POD type, the initial value is indeterminate: <cite>(see §8.5, [dcl.init], for the
|
||||
accurate definitions).</cite></p>
|
||||
|
||||
<pre>int x ; // no initializer. x value is indeterminate.<br>std::string s ; // no initializer, s is default-constructed.<br><br>int y = int() ; <br>// y is initialized using copy-initialization<br>// but the temporary uses an empty set of parentheses as the initializer,<br>// so it is default-constructed.<br>// A default constructed POD type is zero-initialized,<br>// therefore, y == 0.<br><br>void foo ( std::string ) ;<br>foo ( std::string() ) ; <br>// the temporary string is default constructed <br>// as indicated by the initializer () </pre>
|
||||
@ -87,14 +170,11 @@ the object shall be default-initialized (but see below). However, if a <i>decla
|
||||
<p>In order to specify value-initialization of an object we need to use the
|
||||
empty-set initializer: (). </p>
|
||||
|
||||
<p><i>(but recall that the current C++ Standard states that '()' invokes default-initialization,
|
||||
not value-initialization)</i></p>
|
||||
|
||||
<p>As before, a declaration with no intializer specifies default-initialization,
|
||||
and a declaration with a non-empty initializer specifies copy (=xxx) or
|
||||
direct (xxx) initialization. </p>
|
||||
|
||||
<pre>template<class T> void eat(T);<br>int x ; // indeterminate initial value.<br>std::string s; // default-initialized.<br>eat ( int() ) ; // value-initialized<br>eat ( std::string() ) ; // value-initialied</pre>
|
||||
<pre>template<class T> void eat(T);<br>int x ; // indeterminate initial value.<br>std::string s; // default-initialized.<br>eat ( int() ) ; // value-initialized<br>eat ( std::string() ) ; // value-initialized</pre>
|
||||
|
||||
<h4><a name="valueinitsyn">value-initialization</a> syntax</h4>
|
||||
|
||||
@ -102,7 +182,7 @@ not value-initialization)</i></p>
|
||||
parentheses is not permitted by the syntax of initializers because it is
|
||||
parsed as the declaration of a function taking no arguments: </p>
|
||||
|
||||
<pre>int x() ; // declares function int(*)()<br>int y ( int() ) ; // decalares function int(*)( int(*)() )</pre>
|
||||
<pre>int x() ; // declares function int(*)()</pre>
|
||||
|
||||
<p>Thus, the empty () must be put in some other initialization context.</p>
|
||||
|
||||
@ -124,8 +204,50 @@ data member:</p>
|
||||
|
||||
<pre>template<class T> <br>struct W <br>{<br> // value-initialization of 'data' here.<br> W() : data() {}<br> T data ;<br>} ;<br>W<int> w ;<br>// w.data is value-initialized for any type. </pre>
|
||||
|
||||
<p><code>This is the solution supplied by the value_initialized<> template
|
||||
class.</code></p>
|
||||
<p>This is the solution as it was supplied by earlier versions of the
|
||||
<code>value_initialized<T></code> template
|
||||
class. Unfortunately this approach suffered from various compiler issues.</p>
|
||||
|
||||
<h4><a name="compiler_issues">compiler issues</a> </h4>
|
||||
|
||||
Various compilers haven't yet fully implemented value-initialization.
|
||||
So when an object should be <em>value-initialized</em> (according to the C++ Standard),
|
||||
it <em>may</em> in practice still be left uninitialized, because of those
|
||||
compiler issues! It's hard to make a general statement on what those issues
|
||||
are like, because they depend on the compiler you are using, its version number,
|
||||
and the type of object you would like to have value-initialized.
|
||||
Compilers usually support value-initialization for built-in types properly.
|
||||
But objects of user-defined types that involve <em>aggregates</em> may <em>in some cases</em>
|
||||
be partially, or even entirely left uninitialized, when they should be value-initialized.
|
||||
</p>
|
||||
<p>
|
||||
We have encountered issues regarding value-initialization on compilers by
|
||||
Microsoft, Sun, Borland, and GNU. Here is a list of bug reports on those issues:
|
||||
<table summary="Compiler bug reports regarding value-initialization" border="0" cellpadding="7" cellspacing="1" >
|
||||
<tr><td>
|
||||
<a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744">
|
||||
Microsoft Feedback ID 100744 - Value-initialization in new-expression</a>
|
||||
<br>Reported by Pavel Kuznetsov (MetaCommunications Engineering), 2005-07-28
|
||||
<br>
|
||||
<a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30111">
|
||||
GCC Bug 30111 - Value-initialization of POD base class doesn't initialize members</a>
|
||||
<br>Reported by Jonathan Wakely, 2006-12-07
|
||||
<br>
|
||||
<a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33916">
|
||||
GCC Bug 33916 - Default constructor fails to initialize array members</a>
|
||||
<br>Reported by Michael Elizabeth Chastain, 2007-10-26
|
||||
<br>
|
||||
<a href="http://qc.codegear.com/wc/qcmain.aspx?d=51854">
|
||||
Borland Report 51854 - Value-initialization: POD struct should be zero-initialized</a>
|
||||
<br>Reported by Niels Dekker (LKEB, Leiden University Medical Center), 2007-09-11
|
||||
<br>
|
||||
</td></tr></table>
|
||||
</p><p>
|
||||
New versions of <code>value_initialized</code>
|
||||
(Boost release version 1.35 or higher)
|
||||
offer a workaround to these issues: <code>value_initialized</code> will now clear
|
||||
its internal data, prior to constructing the object that it contains.
|
||||
</p>
|
||||
|
||||
<h2><a name="types"></a>Types</h2>
|
||||
|
||||
@ -189,31 +311,78 @@ wrapped object from within a constant wrapper can be avoided if access to
|
||||
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] The C++ Standard, ISO/IEC 14882:98 <br>
|
||||
[2] Plain Old Data
|
||||
[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>
|
||||
in the next version of C++.
|
||||
This would allow a variable <code>var</code> of any DefaultConstructible type
|
||||
<code>T</code> to be <em>value-initialized</em> by doing <code>T var = {}</code>.
|
||||
The papers are listed at Bjarne's web page,
|
||||
<a href="http://www.research.att.com/~bs/WG21.html">My C++ Standards committee papers</a> <br>
|
||||
[2] Scott Meyers, Effective C++, Third Edition, item 6,
|
||||
<em>Explicitly disallow the use of compiler-generated functions you do not want</em>,
|
||||
<a href="http://www.aristeia.com/books.html">Scott Meyers: Books and CDs</a> <br>
|
||||
[3] The C++ Standard, Second edition (2003), ISO/IEC 14882:2003 <br>
|
||||
[4] POD stands for "Plain Old Data"
|
||||
|
||||
<h3><a name="acknowledgements"></a>Acknowledgements</h3>
|
||||
value_initialized was developed by Fernando Cacciola, with help and
|
||||
suggestions from David Abrahams and Darin Adler.<br>
|
||||
Special thanks to Bj<EFBFBD>rn Karlsson who carefully edited and completed this documentation.
|
||||
<pre> </pre>
|
||||
|
||||
<hr>
|
||||
<p>Revised 19 September 2002</p>
|
||||
|
||||
<p>© Copyright boost.org 2002. 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>
|
||||
|
||||
Special thanks to Björn Karlsson who carefully edited and completed this documentation.
|
||||
|
||||
<p>value_initialized was reimplemented by Fernando Cacciola and Niels Dekker
|
||||
for Boost release version 1.35 (2008), offering a workaround to various compiler issues.
|
||||
</p>
|
||||
<p>Developed by <a href="mailto:fernando_cacciola@hotmail.com">Fernando Cacciola</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>.
|
||||
href="http://www.boost.org">www.boost.org</a>.
|
||||
</p>
|
||||
|
||||
<hr>
|
||||
<p>Revised 16 January 2008</p>
|
||||
|
||||
<p>© Copyright Fernando Cacciola, 2002, 2008.</p>
|
||||
|
||||
<p>Distributed under the Boost Software License, Version 1.0. See
|
||||
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
@ -1,4 +1,4 @@
|
||||
// (C) 2002, Fernando Luis Cacciola Carballal.
|
||||
// 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
|
||||
@ -6,12 +6,15 @@
|
||||
//
|
||||
// Test program for "boost/utility/value_init.hpp"
|
||||
//
|
||||
// Initial: 21 Agu 2002
|
||||
// 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>
|
||||
#include <string>
|
||||
|
||||
#include "boost/utility/value_init.hpp"
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
@ -49,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 << ')' ; }
|
||||
@ -60,12 +63,165 @@ struct NonPOD : NonPODBase
|
||||
std::string id ;
|
||||
} ;
|
||||
|
||||
template<class T>
|
||||
void test ( T const& y, T const& z )
|
||||
//
|
||||
// Sample aggregate POD struct type
|
||||
// Some compilers do not correctly value-initialize such a struct, for example:
|
||||
// Borland C++ Report #51854, "Value-initialization: POD struct should be zero-initialized "
|
||||
// http://qc.codegear.com/wc/qcmain.aspx?d=51854
|
||||
//
|
||||
struct AggregatePODStruct
|
||||
{
|
||||
float f;
|
||||
char c;
|
||||
int i;
|
||||
};
|
||||
|
||||
bool operator == ( AggregatePODStruct const& lhs, AggregatePODStruct const& rhs )
|
||||
{ return lhs.f == rhs.f && lhs.c == rhs.c && lhs.i == rhs.i ; }
|
||||
|
||||
//
|
||||
// An aggregate struct that contains an std::string and an int.
|
||||
// Pavel Kuznetsov (MetaCommunications Engineering) used a struct like
|
||||
// this to reproduce the Microsoft Visual C++ compiler bug, reported as
|
||||
// Feedback ID 100744, "Value-initialization in new-expression"
|
||||
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744
|
||||
//
|
||||
struct StringAndInt
|
||||
{
|
||||
std::string s;
|
||||
int i;
|
||||
};
|
||||
|
||||
bool operator == ( StringAndInt const& lhs, StringAndInt const& rhs )
|
||||
{ return lhs.s == rhs.s && lhs.i == rhs.i ; }
|
||||
|
||||
|
||||
//
|
||||
// A struct that has an explicit (user defined) destructor.
|
||||
// Some compilers do not correctly value-initialize such a struct, for example:
|
||||
// Microsoft Visual C++, Feedback ID 100744, "Value-initialization in new-expression"
|
||||
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744
|
||||
//
|
||||
struct StructWithDestructor
|
||||
{
|
||||
int i;
|
||||
~StructWithDestructor() {}
|
||||
};
|
||||
|
||||
bool operator == ( StructWithDestructor const& lhs, StructWithDestructor const& rhs )
|
||||
{ return lhs.i == rhs.i ; }
|
||||
|
||||
|
||||
//
|
||||
// A struct that has a virtual function.
|
||||
// Some compilers do not correctly value-initialize such a struct either, for example:
|
||||
// Microsoft Visual C++, Feedback ID 100744, "Value-initialization in new-expression"
|
||||
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744
|
||||
//
|
||||
struct StructWithVirtualFunction
|
||||
{
|
||||
int i;
|
||||
virtual void VirtualFunction();
|
||||
};
|
||||
|
||||
void StructWithVirtualFunction::VirtualFunction()
|
||||
{
|
||||
}
|
||||
|
||||
bool operator == ( StructWithVirtualFunction const& lhs, StructWithVirtualFunction const& rhs )
|
||||
{ return lhs.i == rhs.i ; }
|
||||
|
||||
|
||||
//
|
||||
// A struct that is derived from an aggregate POD struct.
|
||||
// Some compilers do not correctly value-initialize such a struct, for example:
|
||||
// GCC Bugzilla Bug 30111, "Value-initialization of POD base class doesn't initialize members",
|
||||
// reported by Jonathan Wakely, http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30111
|
||||
//
|
||||
struct DerivedFromAggregatePODStruct : AggregatePODStruct
|
||||
{
|
||||
DerivedFromAggregatePODStruct() : AggregatePODStruct() {}
|
||||
};
|
||||
|
||||
//
|
||||
// A struct that wraps an aggregate POD struct as data member.
|
||||
//
|
||||
struct AggregatePODStructWrapper
|
||||
{
|
||||
AggregatePODStructWrapper() : dataMember() {}
|
||||
AggregatePODStruct dataMember;
|
||||
};
|
||||
|
||||
bool operator == ( AggregatePODStructWrapper const& lhs, AggregatePODStructWrapper const& rhs )
|
||||
{ return lhs.dataMember == rhs.dataMember ; }
|
||||
|
||||
typedef unsigned char ArrayOfBytes[256];
|
||||
|
||||
|
||||
//
|
||||
// A struct that allows testing whether the appropriate copy functions are called.
|
||||
//
|
||||
struct CopyFunctionCallTester
|
||||
{
|
||||
bool is_copy_constructed;
|
||||
bool is_assignment_called;
|
||||
|
||||
CopyFunctionCallTester()
|
||||
: is_copy_constructed(false), is_assignment_called(false) {}
|
||||
|
||||
CopyFunctionCallTester(const CopyFunctionCallTester & )
|
||||
: is_copy_constructed(true), is_assignment_called(false) {}
|
||||
|
||||
CopyFunctionCallTester & operator=(const CopyFunctionCallTester & )
|
||||
{
|
||||
is_assignment_called = true ;
|
||||
return *this ;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
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.
|
||||
// Returns true on success.
|
||||
//
|
||||
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 ) ;
|
||||
@ -77,6 +233,16 @@ void test ( T const& y, T const& z )
|
||||
x_c_ref = z ;
|
||||
BOOST_CHECK ( x_c == z ) ;
|
||||
|
||||
boost::value_initialized<T> const copy1 = x;
|
||||
BOOST_CHECK ( boost::get(copy1) == boost::get(x) ) ;
|
||||
|
||||
boost::value_initialized<T> copy2;
|
||||
copy2 = x;
|
||||
BOOST_CHECK ( boost::get(copy2) == boost::get(x) ) ;
|
||||
|
||||
boost::shared_ptr<boost::value_initialized<T> > ptr( new boost::value_initialized<T> );
|
||||
BOOST_CHECK ( y == *ptr ) ;
|
||||
|
||||
#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
|
||||
boost::value_initialized<T const> cx ;
|
||||
BOOST_CHECK ( y == cx ) ;
|
||||
@ -86,14 +252,71 @@ void test ( T const& y, T const& z )
|
||||
BOOST_CHECK ( y == cx_c ) ;
|
||||
BOOST_CHECK ( y == boost::get(cx_c) ) ;
|
||||
#endif
|
||||
|
||||
return boost::minimal_test::errors_counter() == counter_before_test ;
|
||||
}
|
||||
|
||||
int test_main(int, char **)
|
||||
{
|
||||
test( 0,1234 ) ;
|
||||
test( 0.0,12.34 ) ;
|
||||
test( POD(0,0,0.0), POD('a',1234,56.78) ) ;
|
||||
test( NonPOD( std::string() ), NonPOD( std::string("something") ) ) ;
|
||||
BOOST_CHECK ( test( 0,1234 ) ) ;
|
||||
BOOST_CHECK ( test( 0.0,12.34 ) ) ;
|
||||
BOOST_CHECK ( test( POD(0,0,0.0), POD('a',1234,56.78) ) ) ;
|
||||
BOOST_CHECK ( test( NonPOD( std::string() ), NonPOD( std::string("something") ) ) ) ;
|
||||
|
||||
NonPOD NonPOD_object( std::string("NonPOD_object") );
|
||||
BOOST_CHECK ( test<NonPOD *>( 0, &NonPOD_object ) ) ;
|
||||
|
||||
AggregatePODStruct zeroInitializedAggregatePODStruct = { 0.0f, '\0', 0 };
|
||||
AggregatePODStruct nonZeroInitializedAggregatePODStruct = { 1.25f, 'a', -1 };
|
||||
BOOST_CHECK ( test(zeroInitializedAggregatePODStruct, nonZeroInitializedAggregatePODStruct) );
|
||||
|
||||
StringAndInt stringAndInt0;
|
||||
StringAndInt stringAndInt1;
|
||||
stringAndInt0.i = 0;
|
||||
stringAndInt1.i = 1;
|
||||
stringAndInt1.s = std::string("1");
|
||||
BOOST_CHECK ( test(stringAndInt0, stringAndInt1) );
|
||||
|
||||
StructWithDestructor structWithDestructor0;
|
||||
StructWithDestructor structWithDestructor1;
|
||||
structWithDestructor0.i = 0;
|
||||
structWithDestructor1.i = 1;
|
||||
BOOST_CHECK ( test(structWithDestructor0, structWithDestructor1) );
|
||||
|
||||
StructWithVirtualFunction structWithVirtualFunction0;
|
||||
StructWithVirtualFunction structWithVirtualFunction1;
|
||||
structWithVirtualFunction0.i = 0;
|
||||
structWithVirtualFunction1.i = 1;
|
||||
BOOST_CHECK ( test(structWithVirtualFunction0, structWithVirtualFunction1) );
|
||||
|
||||
DerivedFromAggregatePODStruct derivedFromAggregatePODStruct0;
|
||||
DerivedFromAggregatePODStruct derivedFromAggregatePODStruct1;
|
||||
static_cast<AggregatePODStruct &>(derivedFromAggregatePODStruct0) = zeroInitializedAggregatePODStruct;
|
||||
static_cast<AggregatePODStruct &>(derivedFromAggregatePODStruct1) = nonZeroInitializedAggregatePODStruct;
|
||||
BOOST_CHECK ( test(derivedFromAggregatePODStruct0, derivedFromAggregatePODStruct1) );
|
||||
|
||||
AggregatePODStructWrapper aggregatePODStructWrapper0;
|
||||
AggregatePODStructWrapper aggregatePODStructWrapper1;
|
||||
aggregatePODStructWrapper0.dataMember = zeroInitializedAggregatePODStruct;
|
||||
aggregatePODStructWrapper1.dataMember = nonZeroInitializedAggregatePODStruct;
|
||||
BOOST_CHECK ( test(aggregatePODStructWrapper0, aggregatePODStructWrapper1) );
|
||||
|
||||
ArrayOfBytes zeroInitializedArrayOfBytes = { 0 };
|
||||
boost::value_initialized<ArrayOfBytes> valueInitializedArrayOfBytes;
|
||||
BOOST_CHECK (std::memcmp(get(valueInitializedArrayOfBytes), zeroInitializedArrayOfBytes, sizeof(ArrayOfBytes)) == 0);
|
||||
|
||||
boost::value_initialized<CopyFunctionCallTester> copyFunctionCallTester1;
|
||||
BOOST_CHECK ( ! get(copyFunctionCallTester1).is_copy_constructed);
|
||||
BOOST_CHECK ( ! get(copyFunctionCallTester1).is_assignment_called);
|
||||
|
||||
boost::value_initialized<CopyFunctionCallTester> copyFunctionCallTester2 = boost::value_initialized<CopyFunctionCallTester>(copyFunctionCallTester1);
|
||||
BOOST_CHECK ( get(copyFunctionCallTester2).is_copy_constructed);
|
||||
BOOST_CHECK ( ! get(copyFunctionCallTester2).is_assignment_called);
|
||||
|
||||
boost::value_initialized<CopyFunctionCallTester> copyFunctionCallTester3;
|
||||
copyFunctionCallTester3 = boost::value_initialized<CopyFunctionCallTester>(copyFunctionCallTester1);
|
||||
BOOST_CHECK ( ! get(copyFunctionCallTester3).is_copy_constructed);
|
||||
BOOST_CHECK ( get(copyFunctionCallTester3).is_assignment_called);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -101,7 +324,3 @@ int test_main(int, char **)
|
||||
|
||||
unsigned int expected_failures = 0;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
// (C) 2002, Fernando Luis Cacciola Carballal.
|
||||
// Copyright 2002, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
|
@ -1,4 +1,4 @@
|
||||
// (C) 2002, Fernando Luis Cacciola Carballal.
|
||||
// Copyright 2002, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
|
@ -1,4 +1,4 @@
|
||||
// (C) 2002, Fernando Luis Cacciola Carballal.
|
||||
// Copyright 2002, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
|
126
verify_test.cpp
Normal file
126
verify_test.cpp
Normal file
@ -0,0 +1,126 @@
|
||||
//
|
||||
// verify_test.cpp - a test for BOOST_VERIFY
|
||||
//
|
||||
// Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
|
||||
// Copyright (c) 2007 Peter Dimov
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
int f( int & x )
|
||||
{
|
||||
return ++x;
|
||||
}
|
||||
|
||||
void test_default()
|
||||
{
|
||||
int x = 1;
|
||||
|
||||
BOOST_VERIFY( 1 );
|
||||
BOOST_VERIFY( x == 1 );
|
||||
BOOST_VERIFY( ++x );
|
||||
BOOST_VERIFY( f(x) );
|
||||
BOOST_VERIFY( &x );
|
||||
|
||||
BOOST_TEST( x == 3 );
|
||||
}
|
||||
|
||||
#define BOOST_DISABLE_ASSERTS
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
void test_disabled()
|
||||
{
|
||||
int x = 1;
|
||||
|
||||
BOOST_VERIFY( 1 );
|
||||
BOOST_VERIFY( x == 1 );
|
||||
BOOST_VERIFY( ++x );
|
||||
BOOST_VERIFY( f(x) );
|
||||
BOOST_VERIFY( &x );
|
||||
|
||||
BOOST_TEST( x == 3 );
|
||||
|
||||
BOOST_VERIFY( 0 );
|
||||
BOOST_VERIFY( !x );
|
||||
BOOST_VERIFY( x == 0 );
|
||||
BOOST_VERIFY( !++x );
|
||||
BOOST_VERIFY( !f(x) );
|
||||
|
||||
BOOST_TEST( x == 5 );
|
||||
|
||||
void * p = 0;
|
||||
BOOST_VERIFY( p );
|
||||
}
|
||||
|
||||
#undef BOOST_DISABLE_ASSERTS
|
||||
|
||||
#define BOOST_ENABLE_ASSERT_HANDLER
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <cstdio>
|
||||
|
||||
int handler_invoked = 0;
|
||||
|
||||
void boost::assertion_failed(char const * expr, char const * function, char const * file, long line)
|
||||
{
|
||||
#if !defined(BOOST_NO_STDC_NAMESPACE)
|
||||
using std::printf;
|
||||
#endif
|
||||
|
||||
printf("Expression: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, function, file, line);
|
||||
++handler_invoked;
|
||||
}
|
||||
|
||||
struct X
|
||||
{
|
||||
static bool f()
|
||||
{
|
||||
BOOST_VERIFY( 0 );
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
void test_handler()
|
||||
{
|
||||
int x = 1;
|
||||
|
||||
BOOST_VERIFY( 1 );
|
||||
BOOST_VERIFY( x == 1 );
|
||||
BOOST_VERIFY( ++x );
|
||||
BOOST_VERIFY( f(x) );
|
||||
BOOST_VERIFY( &x );
|
||||
|
||||
BOOST_TEST( x == 3 );
|
||||
|
||||
BOOST_VERIFY( 0 );
|
||||
BOOST_VERIFY( !x );
|
||||
BOOST_VERIFY( x == 0 );
|
||||
BOOST_VERIFY( !++x );
|
||||
BOOST_VERIFY( !f(x) );
|
||||
|
||||
BOOST_TEST( x == 5 );
|
||||
|
||||
void * p = 0;
|
||||
BOOST_VERIFY( p );
|
||||
|
||||
BOOST_VERIFY( X::f() );
|
||||
|
||||
BOOST_TEST( handler_invoked == 8 );
|
||||
}
|
||||
|
||||
#undef BOOST_ENABLE_ASSERT_HANDLER
|
||||
|
||||
int main()
|
||||
{
|
||||
test_default();
|
||||
test_disabled();
|
||||
test_handler();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user