From e9a53f2e69b9a74bfa0445907fb87d87beb624d4 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Sun, 1 Sep 2013 16:31:16 +0000 Subject: [PATCH 01/39] Extracted BOOST_EXPLICIT_OPERATOR_BOOL macro from Boost.Log. [SVN r85543] --- doc/explicit_operator_bool.qbk | 68 ++++++++++ .../boost/utility/explicit_operator_bool.hpp | 128 ++++++++++++++++++ test/explicit_operator_bool.cpp | 54 ++++++++ ...it_operator_bool_compile_fail_conv_int.cpp | 40 ++++++ ..._operator_bool_compile_fail_conv_pvoid.cpp | 40 ++++++ ...icit_operator_bool_compile_fail_delete.cpp | 40 ++++++ ...licit_operator_bool_compile_fail_shift.cpp | 40 ++++++ 7 files changed, 410 insertions(+) create mode 100644 doc/explicit_operator_bool.qbk create mode 100644 include/boost/utility/explicit_operator_bool.hpp create mode 100644 test/explicit_operator_bool.cpp create mode 100644 test/explicit_operator_bool_compile_fail_conv_int.cpp create mode 100644 test/explicit_operator_bool_compile_fail_conv_pvoid.cpp create mode 100644 test/explicit_operator_bool_compile_fail_delete.cpp create mode 100644 test/explicit_operator_bool_compile_fail_shift.cpp diff --git a/doc/explicit_operator_bool.qbk b/doc/explicit_operator_bool.qbk new file mode 100644 index 0000000..4a7e4b1 --- /dev/null +++ b/doc/explicit_operator_bool.qbk @@ -0,0 +1,68 @@ +[/ + / Copyright (c) 2013 Andrey Semashev + / + / 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) + /] + +[article BOOST_EXPLICIT_OPERATOR_BOOL and BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL + [quickbook 1.5] + [authors [Semashev, Andrey]] + [copyright 2013 Andrey Semashev] + [license + 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]) + ] +] + +[/===============] +[section Overview] +[/===============] + +`BOOST_EXPLICIT_OPERATOR_BOOL()` and `BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()` are compatibility helper macros that expand to an explicit conversion operator to `bool`. For compilers not supporting explicit conversion operators introduced in C++11 the macros expand to a conversion operator that implements the [@http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool safe bool idiom]. In case if the compiler is not able to handle safe bool idiom well the macros expand to a regular conversion operator to `bool`. + +[endsect] + + +[/===============] +[section Examples] +[/===============] + +Both macros are intended to be placed within a user's class definition. The generated conversion operators will be implemented in terms of `operator!()` that should be defined by user in this class. In case of `BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()` the generated conversion operator will be declared `constexpr` which requires the corresponding `operator!()` to also be `constexpr`. + + template< typename T > + class my_ptr + { + T* m_p; + + public: + BOOST_EXPLICIT_OPERATOR_BOOL() + + bool operator!() const + { + return !m_p; + } + }; + +Now `my_ptr` can be used in conditional expressions, similarly to a regular pointer: + + my_ptr< int > p; + if (p) + std::cout << "true" << std::endl; + +[endsect] + +[/===============] +[section History] +[/===============] + +[heading boost 1.55] + +* The macro was extracted from Boost.Log. + +[endsect] + + + + diff --git a/include/boost/utility/explicit_operator_bool.hpp b/include/boost/utility/explicit_operator_bool.hpp new file mode 100644 index 0000000..8843e0c --- /dev/null +++ b/include/boost/utility/explicit_operator_bool.hpp @@ -0,0 +1,128 @@ +/* + * Copyright Andrey Semashev 2007 - 2013. + * 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) + */ +/*! + * \file explicit_operator_bool.hpp + * \author Andrey Semashev + * \date 08.03.2009 + * + * This header defines a compatibility macro that implements an unspecified + * \c bool operator idiom, which is superseded with explicit conversion operators in + * C++11. + */ + +#ifndef BOOST_UTILITY_EXPLICIT_OPERATOR_BOOL_HPP_INCLUDED_ +#define BOOST_UTILITY_EXPLICIT_OPERATOR_BOOL_HPP_INCLUDED_ + +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +#if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) + +/*! + * \brief The macro defines an explicit operator of conversion to \c bool + * + * The macro should be used inside the definition of a class that has to + * support the conversion. The class should also implement operator!, + * in terms of which the conversion operator will be implemented. + */ +#define BOOST_EXPLICIT_OPERATOR_BOOL()\ + BOOST_FORCEINLINE explicit operator bool () const\ + {\ + return !this->operator! ();\ + } + +/*! + * \brief The macro defines a constexpr explicit operator of conversion to \c bool + * + * The macro should be used inside the definition of a class that has to + * support the conversion. The class should also implement operator!, + * in terms of which the conversion operator will be implemented. + */ +#define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ + BOOST_FORCEINLINE BOOST_CONSTEXPR explicit operator bool () const\ + {\ + return !this->operator! ();\ + } + +#else // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) + +#if (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG) +// Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it +#define BOOST_NO_UNSPECIFIED_BOOL +#endif // (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG) + +#if !defined(BOOST_NO_UNSPECIFIED_BOOL) + +namespace boost { + +namespace detail { + +#if !defined(_MSC_VER) + + struct unspecified_bool + { + // NOTE TO THE USER: If you see this in error messages then you tried + // to apply an unsupported operator on the object that supports + // explicit conversion to bool. + struct OPERATORS_NOT_ALLOWED; + static void true_value(OPERATORS_NOT_ALLOWED*) {} + }; + typedef void (*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*); + +#else + + // MSVC is too eager to convert pointer to function to void* even though it shouldn't + struct unspecified_bool + { + // NOTE TO THE USER: If you see this in error messages then you tried + // to apply an unsupported operator on the object that supports + // explicit conversion to bool. + struct OPERATORS_NOT_ALLOWED; + void true_value(OPERATORS_NOT_ALLOWED*) {} + }; + typedef void (unspecified_bool::*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*); + +#endif + +} // namespace detail + +} // namespace boost + +#define BOOST_EXPLICIT_OPERATOR_BOOL()\ + BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const\ + {\ + return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ + } + +#define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ + BOOST_FORCEINLINE BOOST_CONSTEXPR operator boost::detail::unspecified_bool_type () const\ + {\ + return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ + } + +#else // !defined(BOOST_NO_UNSPECIFIED_BOOL) + +#define BOOST_EXPLICIT_OPERATOR_BOOL()\ + BOOST_FORCEINLINE operator bool () const\ + {\ + return !this->operator! ();\ + } + +#define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ + BOOST_FORCEINLINE BOOST_CONSTEXPR operator bool () const\ + {\ + return !this->operator! ();\ + } + +#endif // !defined(BOOST_NO_UNSPECIFIED_BOOL) + +#endif // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) + +#endif // BOOST_UTILITY_EXPLICIT_OPERATOR_BOOL_HPP_INCLUDED_ diff --git a/test/explicit_operator_bool.cpp b/test/explicit_operator_bool.cpp new file mode 100644 index 0000000..07dd01b --- /dev/null +++ b/test/explicit_operator_bool.cpp @@ -0,0 +1,54 @@ +/* + * Copyright Andrey Semashev 2007 - 2013. + * 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) + */ +/*! + * \file explicit_operator_bool_compile.cpp + * \author Andrey Semashev + * \date 17.07.2010 + * + * \brief This test checks that explicit operator bool can be used in + * the valid contexts. + */ + +#define BOOST_TEST_MODULE explicit_operator_bool_compile + +#include + +namespace { + + // A test object that has the operator of explicit conversion to bool + struct checkable1 + { + BOOST_EXPLICIT_OPERATOR_BOOL() + bool operator! () const + { + return false; + } + }; + + struct checkable2 + { + BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL() + BOOST_CONSTEXPR bool operator! () const + { + return false; + } + }; + +} // namespace + +int main(int, char*[]) +{ + checkable1 val1; + if (val1) + { + checkable2 val2; + if (val2) + return 0; + } + + return 1; +} diff --git a/test/explicit_operator_bool_compile_fail_conv_int.cpp b/test/explicit_operator_bool_compile_fail_conv_int.cpp new file mode 100644 index 0000000..384f2ab --- /dev/null +++ b/test/explicit_operator_bool_compile_fail_conv_int.cpp @@ -0,0 +1,40 @@ +/* + * Copyright Andrey Semashev 2007 - 2013. + * 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) + */ +/*! + * \file explicit_operator_bool_compile_fail_conv_int.cpp + * \author Andrey Semashev + * \date 17.07.2010 + * + * \brief This test checks that explicit operator bool cannot be used in + * an unintended context. + */ + +#define BOOST_TEST_MODULE explicit_operator_bool_compile_fail_conv_int + +#include + +namespace { + + // A test object that has the operator of explicit conversion to bool + struct checkable + { + BOOST_EXPLICIT_OPERATOR_BOOL() + bool operator! () const + { + return false; + } + }; + +} // namespace + +int main(int, char*[]) +{ + checkable val; + int n = val; + + return 0; +} diff --git a/test/explicit_operator_bool_compile_fail_conv_pvoid.cpp b/test/explicit_operator_bool_compile_fail_conv_pvoid.cpp new file mode 100644 index 0000000..0c007ba --- /dev/null +++ b/test/explicit_operator_bool_compile_fail_conv_pvoid.cpp @@ -0,0 +1,40 @@ +/* + * Copyright Andrey Semashev 2007 - 2013. + * 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) + */ +/*! + * \file explicit_operator_bool_compile_fail_conv_pvoid.cpp + * \author Andrey Semashev + * \date 17.07.2010 + * + * \brief This test checks that explicit operator bool cannot be used in + * an unintended context. + */ + +#define BOOST_TEST_MODULE explicit_operator_bool_compile_fail_conv_pvoid + +#include + +namespace { + + // A test object that has the operator of explicit conversion to bool + struct checkable + { + BOOST_EXPLICIT_OPERATOR_BOOL() + bool operator! () const + { + return false; + } + }; + +} // namespace + +int main(int, char*[]) +{ + checkable val; + void* p = val; + + return 0; +} diff --git a/test/explicit_operator_bool_compile_fail_delete.cpp b/test/explicit_operator_bool_compile_fail_delete.cpp new file mode 100644 index 0000000..d780f94 --- /dev/null +++ b/test/explicit_operator_bool_compile_fail_delete.cpp @@ -0,0 +1,40 @@ +/* + * Copyright Andrey Semashev 2007 - 2013. + * 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) + */ +/*! + * \file explicit_operator_bool_compile_fail_delete.cpp + * \author Andrey Semashev + * \date 17.07.2010 + * + * \brief This test checks that explicit operator bool cannot be used in + * an unintended context. + */ + +#define BOOST_TEST_MODULE util_explicit_operator_bool_delete + +#include + +namespace { + + // A test object that has the operator of explicit conversion to bool + struct checkable + { + BOOST_EXPLICIT_OPERATOR_BOOL() + bool operator! () const + { + return false; + } + }; + +} // namespace + +int main(int, char*[]) +{ + checkable val; + delete val; + + return 0; +} diff --git a/test/explicit_operator_bool_compile_fail_shift.cpp b/test/explicit_operator_bool_compile_fail_shift.cpp new file mode 100644 index 0000000..ce563a6 --- /dev/null +++ b/test/explicit_operator_bool_compile_fail_shift.cpp @@ -0,0 +1,40 @@ +/* + * Copyright Andrey Semashev 2007 - 2013. + * 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) + */ +/*! + * \file explicit_operator_bool_compile_fail_shift.cpp + * \author Andrey Semashev + * \date 17.07.2010 + * + * \brief This test checks that explicit operator bool cannot be used in + * an unintended context. + */ + +#define BOOST_TEST_MODULE explicit_operator_bool_compile_fail_shift + +#include + +namespace { + + // A test object that has the operator of explicit conversion to bool + struct checkable + { + BOOST_EXPLICIT_OPERATOR_BOOL() + bool operator! () const + { + return false; + } + }; + +} // namespace + +int main(int, char*[]) +{ + checkable val; + val << 2; + + return 0; +} From 1c247b7fe7449be4cf83840caed869e0a45f9d82 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Thu, 5 Sep 2013 08:28:24 +0000 Subject: [PATCH 02/39] Attempt to work around explicit_operator_bool_compile_fail_conv_pvoid test failure for VACPP. [SVN r85570] --- include/boost/utility/explicit_operator_bool.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/utility/explicit_operator_bool.hpp b/include/boost/utility/explicit_operator_bool.hpp index 8843e0c..650caff 100644 --- a/include/boost/utility/explicit_operator_bool.hpp +++ b/include/boost/utility/explicit_operator_bool.hpp @@ -64,7 +64,7 @@ namespace boost { namespace detail { -#if !defined(_MSC_VER) +#if !defined(_MSC_VER) && !defined(__IBMCPP__) struct unspecified_bool { @@ -78,7 +78,7 @@ namespace detail { #else - // MSVC is too eager to convert pointer to function to void* even though it shouldn't + // MSVC and VACPP are too eager to convert pointer to function to void* even though they shouldn't struct unspecified_bool { // NOTE TO THE USER: If you see this in error messages then you tried From aaadc128f3b63cc94c5c5ba4fce053b433f09df8 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Sat, 26 Apr 2014 15:11:35 +0400 Subject: [PATCH 03/39] Added a new macro BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT, which implements a noexcept operator. Also added explicit noexcept specification for the constexpr macro. --- doc/explicit_operator_bool.qbk | 8 +- .../boost/utility/explicit_operator_bool.hpp | 31 ++++++- test/explicit_operator_bool.cpp | 4 +- test/explicit_operator_bool_noexcept.cpp | 89 +++++++++++++++++++ 4 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 test/explicit_operator_bool_noexcept.cpp diff --git a/doc/explicit_operator_bool.qbk b/doc/explicit_operator_bool.qbk index 4a7e4b1..64c5be4 100644 --- a/doc/explicit_operator_bool.qbk +++ b/doc/explicit_operator_bool.qbk @@ -5,7 +5,7 @@ / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) /] -[article BOOST_EXPLICIT_OPERATOR_BOOL and BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL +[article BOOST_EXPLICIT_OPERATOR_BOOL, BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT and BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL [quickbook 1.5] [authors [Semashev, Andrey]] [copyright 2013 Andrey Semashev] @@ -20,7 +20,7 @@ [section Overview] [/===============] -`BOOST_EXPLICIT_OPERATOR_BOOL()` and `BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()` are compatibility helper macros that expand to an explicit conversion operator to `bool`. For compilers not supporting explicit conversion operators introduced in C++11 the macros expand to a conversion operator that implements the [@http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool safe bool idiom]. In case if the compiler is not able to handle safe bool idiom well the macros expand to a regular conversion operator to `bool`. +`BOOST_EXPLICIT_OPERATOR_BOOL()`, `BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()` and `BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()` are compatibility helper macros that expand to an explicit conversion operator to `bool`. For compilers not supporting explicit conversion operators introduced in C++11 the macros expand to a conversion operator that implements the [@http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool safe bool idiom]. In case if the compiler is not able to handle safe bool idiom well the macros expand to a regular conversion operator to `bool`. [endsect] @@ -62,7 +62,3 @@ Now `my_ptr` can be used in conditional expressions, similarly to a regular poin * The macro was extracted from Boost.Log. [endsect] - - - - diff --git a/include/boost/utility/explicit_operator_bool.hpp b/include/boost/utility/explicit_operator_bool.hpp index 650caff..e16f34d 100644 --- a/include/boost/utility/explicit_operator_bool.hpp +++ b/include/boost/utility/explicit_operator_bool.hpp @@ -38,6 +38,19 @@ return !this->operator! ();\ } +/*! + * \brief The macro defines a noexcept explicit operator of conversion to \c bool + * + * The macro should be used inside the definition of a class that has to + * support the conversion. The class should also implement operator!, + * in terms of which the conversion operator will be implemented. + */ +#define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\ + BOOST_FORCEINLINE explicit operator bool () const BOOST_NOEXCEPT\ + {\ + return !this->operator! ();\ + } + /*! * \brief The macro defines a constexpr explicit operator of conversion to \c bool * @@ -46,7 +59,7 @@ * in terms of which the conversion operator will be implemented. */ #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ - BOOST_FORCEINLINE BOOST_CONSTEXPR explicit operator bool () const\ + BOOST_FORCEINLINE BOOST_CONSTEXPR explicit operator bool () const BOOST_NOEXCEPT\ {\ return !this->operator! ();\ } @@ -101,8 +114,14 @@ namespace detail { return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ } +#define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\ + BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\ + {\ + return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ + } + #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ - BOOST_FORCEINLINE BOOST_CONSTEXPR operator boost::detail::unspecified_bool_type () const\ + BOOST_FORCEINLINE BOOST_CONSTEXPR operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\ {\ return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ } @@ -115,8 +134,14 @@ namespace detail { return !this->operator! ();\ } +#define BOOST_EXPLICIT_OPERATOR_BOOL()\ + BOOST_FORCEINLINE operator bool () const BOOST_NOEXCEPT\ + {\ + return !this->operator! ();\ + } + #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ - BOOST_FORCEINLINE BOOST_CONSTEXPR operator bool () const\ + BOOST_FORCEINLINE BOOST_CONSTEXPR operator bool () const BOOST_NOEXCEPT\ {\ return !this->operator! ();\ } diff --git a/test/explicit_operator_bool.cpp b/test/explicit_operator_bool.cpp index 07dd01b..5e85259 100644 --- a/test/explicit_operator_bool.cpp +++ b/test/explicit_operator_bool.cpp @@ -5,7 +5,7 @@ * http://www.boost.org/LICENSE_1_0.txt) */ /*! - * \file explicit_operator_bool_compile.cpp + * \file explicit_operator_bool.cpp * \author Andrey Semashev * \date 17.07.2010 * @@ -13,7 +13,7 @@ * the valid contexts. */ -#define BOOST_TEST_MODULE explicit_operator_bool_compile +#define BOOST_TEST_MODULE explicit_operator_bool #include diff --git a/test/explicit_operator_bool_noexcept.cpp b/test/explicit_operator_bool_noexcept.cpp new file mode 100644 index 0000000..c645cca --- /dev/null +++ b/test/explicit_operator_bool_noexcept.cpp @@ -0,0 +1,89 @@ +/* + * Copyright Andrey Semashev 2007 - 2013. + * 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) + */ +/*! + * \file explicit_operator_bool_noexcept.cpp + * \author Andrey Semashev + * \date 26.04.2014 + * + * \brief This test checks that explicit operator bool is noexcept when possible. + */ + +#define BOOST_TEST_MODULE explicit_operator_bool_noexcept + +#include + +#if !defined(BOOST_NO_CXX11_NOEXCEPT) + +#include +#include + +namespace { + + struct checkable1 + { + BOOST_EXPLICIT_OPERATOR_BOOL() + bool operator! () const + { + return false; + } + }; + + struct checkable2 + { + BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL() + BOOST_CONSTEXPR bool operator! () const + { + return false; + } + }; + + struct noexcept_checkable1 + { + BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT() + bool operator! () const noexcept + { + return false; + } + }; + + struct noexcept_checkable2 + { + BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL() + BOOST_CONSTEXPR bool operator! () const noexcept + { + return false; + } + }; + +} // namespace + +int main(int, char*[]) +{ + checkable1 val1; + checkable2 val2; + + noexcept_checkable1 noexcept_val1; + noexcept_checkable2 noexcept_val2; + + BOOST_TEST(!noexcept(static_cast< bool >(val1))); + // constexpr functions are implicitly noexcept + BOOST_TEST(noexcept(static_cast< bool >(val2))); + + BOOST_TEST(noexcept(static_cast< bool >(noexcept_val1))); + BOOST_TEST(noexcept(static_cast< bool >(noexcept_val2))); + + return boost::report_errors(); +} + +#else + +int main(int, char*[]) +{ + return 0; +} + +#endif From db06972ba318bec74862416c6ac446644b9036d8 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Thu, 5 Sep 2013 18:25:41 +0000 Subject: [PATCH 04/39] Extracted empty_deleter to Boost.Utility. [SVN r85577] --- include/boost/utility/empty_deleter.hpp | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 include/boost/utility/empty_deleter.hpp diff --git a/include/boost/utility/empty_deleter.hpp b/include/boost/utility/empty_deleter.hpp new file mode 100644 index 0000000..ba919cd --- /dev/null +++ b/include/boost/utility/empty_deleter.hpp @@ -0,0 +1,43 @@ +/* + * Copyright Andrey Semashev 2007 - 2013. + * 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) + */ +/*! + * \file empty_deleter.hpp + * \author Andrey Semashev + * \date 22.04.2007 + * + * This header contains an \c empty_deleter implementation. This is an empty + * function object that receives a pointer and does nothing with it. + * Such empty deletion strategy may be convenient, for example, when + * constructing shared_ptrs that point to some object that should not be + * deleted (i.e. a variable on the stack or some global singleton, like std::cout). + */ + +#ifndef BOOST_UTILITY_EMPTY_DELETER_HPP_INCLUDED_ +#define BOOST_UTILITY_EMPTY_DELETER_HPP_INCLUDED_ + +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +//! A function object that does nothing and can be used as an empty deleter for \c shared_ptr +struct empty_deleter +{ + //! Function object result type + typedef void result_type; + /*! + * Does nothing + */ + void operator() (const volatile void*) const BOOST_NOEXCEPT {} +}; + +} // namespace boost + +#endif // BOOST_UTILITY_EMPTY_DELETER_HPP_INCLUDED_ From a3d73aed7547ecc9eec8705c752d7b90beda651b Mon Sep 17 00:00:00 2001 From: Joseph Gauterin Date: Sat, 5 Jul 2008 11:16:38 +0000 Subject: [PATCH 05/39] Moved utility\swap to the trunk, as discussed in trac issue #2056. [SVN r47093] --- include/boost/utility/swap.hpp | 51 +++++++++ swap.html | 94 +++++++++++++++ swap/test/Jamfile.v2 | 53 +++++++++ swap/test/lib_header_1.cpp | 18 +++ swap/test/lib_header_2.cpp | 20 ++++ swap/test/mixed_headers_1.cpp | 20 ++++ swap/test/mixed_headers_2.cpp | 20 ++++ swap/test/primitive.cpp | 44 +++++++ swap/test/root_header_1.cpp | 18 +++ swap/test/root_header_2.cpp | 20 ++++ swap/test/specialized_in_boost.cpp | 72 ++++++++++++ swap/test/specialized_in_global.cpp | 60 ++++++++++ swap/test/specialized_in_other.cpp | 72 ++++++++++++ swap/test/specialized_in_std.cpp | 70 ++++++++++++ swap/test/swap_arrays.cpp | 78 +++++++++++++ swap/test/swap_test_class.hpp | 171 ++++++++++++++++++++++++++++ 16 files changed, 881 insertions(+) create mode 100644 include/boost/utility/swap.hpp create mode 100644 swap.html create mode 100644 swap/test/Jamfile.v2 create mode 100644 swap/test/lib_header_1.cpp create mode 100644 swap/test/lib_header_2.cpp create mode 100644 swap/test/mixed_headers_1.cpp create mode 100644 swap/test/mixed_headers_2.cpp create mode 100644 swap/test/primitive.cpp create mode 100644 swap/test/root_header_1.cpp create mode 100644 swap/test/root_header_2.cpp create mode 100644 swap/test/specialized_in_boost.cpp create mode 100644 swap/test/specialized_in_global.cpp create mode 100644 swap/test/specialized_in_other.cpp create mode 100644 swap/test/specialized_in_std.cpp create mode 100644 swap/test/swap_arrays.cpp create mode 100644 swap/test/swap_test_class.hpp diff --git a/include/boost/utility/swap.hpp b/include/boost/utility/swap.hpp new file mode 100644 index 0000000..952adf5 --- /dev/null +++ b/include/boost/utility/swap.hpp @@ -0,0 +1,51 @@ +// Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin +// +// 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) +// For more information, see http://www.boost.org +// +// Update: +// 29 June 2008 (Added support for built-in arrays.) Niels Dekker + + +#ifndef BOOST_UTILITY_SWAP_HPP +#define BOOST_UTILITY_SWAP_HPP + +#include //for std::swap +#include //for std::size_t + +namespace boost_swap_impl +{ + template + void swap_impl(T& left, T& right) + { + using std::swap;//use std::swap if argument dependent lookup fails + swap(left,right); + } + + template + void swap_impl(T (& left)[N], T (& right)[N]) + { + for (std::size_t i = 0; i < N; ++i) + { + ::boost_swap_impl::swap_impl(left[i], right[i]); + } + } +} + +namespace boost +{ + namespace swap_adl_barrier + { + template + void swap(T& left, T& right) + { + ::boost_swap_impl::swap_impl(left, right); + } + } + + using swap_adl_barrier::swap; +} + +#endif diff --git a/swap.html b/swap.html new file mode 100644 index 0000000..e317b11 --- /dev/null +++ b/swap.html @@ -0,0 +1,94 @@ +? + + + + + Boost: Swap Documentation + + + + C++ Boost +

Swap

+ +

+ template<class T> void swap(T& left, T& right); +

+ + +

+ The template function boost::swap allows the values of two variables to be swapped, using argument dependent lookup to select a specialized swap function if available. If no specialized swap function is available, std::swap is used. +

+ + +

Rationale

+

+ The generic std::swap function requires that the elements to be swapped are assignable and copy constructible. It is usually implemented using one copy construction and two assignments - this is often both unnecessarily restrictive and unnecessarily slow. In addition, where the generic swap implementation provides only the basic guarantee, specialized swap functions are often able to provide the no-throw exception guarantee (and it is considered best practice to do so where possible1).

+

+ The alternative to using argument dependent lookup in this situation is to provide a template specialization of std::swap for every type that requires a specialized swap. Although this is legal C++, no Boost libraries use this method, whereas many Boost libraries provide specialized swap functions in their own namespaces. +

+

+ boost::swap also supports swapping built-in arrays. Note that std::swap doesn't yet do so, but a request to add an overload of std::swap for built-in arrays has been well-received by the Library Working Group of the C++ Standards Committee2. +

+ + +

Exception Safety

+

+ boost::swap provides the same exception guarantee as the underlying swap function used, with one exception; for an array of type T[n], where n > 1 and the underlying swap function for T provides the strong exception guarantee, boost::swap provides only the basic exception guarantee. +

+ + +

Requirements

+

Either:

+
    +
  • T must be assignable
  • +
  • T must be copy constructible
  • +
+

Or:

+
    +
  • A function with the signature swap(T&,T&) is available via argument dependent lookup
  • +
+

Or:

+
    +
  • A template specialization of std::swap exists for T
  • +
+

Or:

+
    +
  • T is a built-in array of swappable elements
  • +
+ + + +

Portability

+

+ Several older compilers do not support argument dependent lookup – on these compilers boost::swap will call std::swap, ignoring any specialized swap functions that could be found as a result of argument dependent lookup. +

+ + +

Credits

+
    +
  • + Niels Dekker - for implementing and documenting support for built-in arrays +
  • +
  • + Joseph Gauterin - for the initial idea, implementation, tests, and documentation +
  • +
  • + Steven Wanatabe - for the idea to use a barrier namespace, enabling the function to have the name 'swap' without introducing ambiguity or infinite recursion +
  • +
+ + +
+

[1]Scott Meyers, Effective C++ Third Edition, Item 25: "Consider support for a non-throwing swap"

+

[2]LWG issue 809 (std::swap should be overloaded for array types)

+ + +
+

Revised: 3 July 2008

+

+ Copyright 2007, 2008 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0. + (See accompanying file LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.) +

+ + + diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 new file mode 100644 index 0000000..758bdaa --- /dev/null +++ b/swap/test/Jamfile.v2 @@ -0,0 +1,53 @@ +# Copyright (c) 2007, 2008 Joseph Gauterin +# +# 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) + +# bring in rules for testing +import testing ; + +test-suite utility/swap + : + [ compile root_header_1.cpp ] + [ compile root_header_2.cpp ] + [ compile lib_header_1.cpp ] + [ compile lib_header_2.cpp ] + [ compile mixed_headers_1.cpp ] + [ compile mixed_headers_2.cpp ] + [ run primitive.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] + ; + + +# Copyright (c) 2007, 2008 Joseph Gauterin +# +# 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) + +# bring in rules for testing +import testing ; + +test-suite utility/swap + : + [ compile root_header_1.cpp ] + [ compile root_header_2.cpp ] + [ compile lib_header_1.cpp ] + [ compile lib_header_2.cpp ] + [ compile mixed_headers_1.cpp ] + [ compile mixed_headers_2.cpp ] + [ run primitive.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] + ; + + + diff --git a/swap/test/lib_header_1.cpp b/swap/test/lib_header_1.cpp new file mode 100644 index 0000000..ea7c99a --- /dev/null +++ b/swap/test/lib_header_1.cpp @@ -0,0 +1,18 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header compiles as a standalone translation unit + +#include +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header compiles as a standalone translation unit + +#include diff --git a/swap/test/lib_header_2.cpp b/swap/test/lib_header_2.cpp new file mode 100644 index 0000000..0c9e409 --- /dev/null +++ b/swap/test/lib_header_2.cpp @@ -0,0 +1,20 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header include guards work correctly + +#include +#include +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header include guards work correctly + +#include +#include diff --git a/swap/test/mixed_headers_1.cpp b/swap/test/mixed_headers_1.cpp new file mode 100644 index 0000000..0c7571e --- /dev/null +++ b/swap/test/mixed_headers_1.cpp @@ -0,0 +1,20 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap headers work when both are included + +#include +#include +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap headers work when both are included + +#include +#include diff --git a/swap/test/mixed_headers_2.cpp b/swap/test/mixed_headers_2.cpp new file mode 100644 index 0000000..217873a --- /dev/null +++ b/swap/test/mixed_headers_2.cpp @@ -0,0 +1,20 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap headers work when both are included + +#include +#include +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap headers work when both are included + +#include +#include diff --git a/swap/test/primitive.cpp b/swap/test/primitive.cpp new file mode 100644 index 0000000..76cd7b0 --- /dev/null +++ b/swap/test/primitive.cpp @@ -0,0 +1,44 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +int test_main(int, char*[]) +{ + int object1 = 1; + int object2 = 2; + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1,2); + BOOST_CHECK_EQUAL(object2,1); + + return 0; +} +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +int test_main(int, char*[]) +{ + int object1 = 1; + int object2 = 2; + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1,2); + BOOST_CHECK_EQUAL(object2,1); + + return 0; +} diff --git a/swap/test/root_header_1.cpp b/swap/test/root_header_1.cpp new file mode 100644 index 0000000..efa0028 --- /dev/null +++ b/swap/test/root_header_1.cpp @@ -0,0 +1,18 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header compiles as a standalone translation unit + +#include +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header compiles as a standalone translation unit + +#include diff --git a/swap/test/root_header_2.cpp b/swap/test/root_header_2.cpp new file mode 100644 index 0000000..d7e00f3 --- /dev/null +++ b/swap/test/root_header_2.cpp @@ -0,0 +1,20 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header include guards work correctly + +#include +#include +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header include guards work correctly + +#include +#include diff --git a/swap/test/specialized_in_boost.cpp b/swap/test/specialized_in_boost.cpp new file mode 100644 index 0000000..bc3840f --- /dev/null +++ b/swap/test/specialized_in_boost.cpp @@ -0,0 +1,72 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace boost +namespace boost +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace boost +namespace boost +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + boost::swap_test_class object1; + boost::swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),0); + + return 0; +} +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace boost +namespace boost +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace boost +namespace boost +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + boost::swap_test_class object1; + boost::swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),0); + + return 0; +} diff --git a/swap/test/specialized_in_global.cpp b/swap/test/specialized_in_global.cpp new file mode 100644 index 0000000..7f46187 --- /dev/null +++ b/swap/test/specialized_in_global.cpp @@ -0,0 +1,60 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + +//Provide swap function in gloabl namespace +void swap(swap_test_class& left, swap_test_class& right) +{ + left.swap(right); +} + +int test_main(int, char*[]) +{ + swap_test_class object1; + swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); + + return 0; +} +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + +//Provide swap function in gloabl namespace +void swap(swap_test_class& left, swap_test_class& right) +{ + left.swap(right); +} + +int test_main(int, char*[]) +{ + swap_test_class object1; + swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); + + return 0; +} diff --git a/swap/test/specialized_in_other.cpp b/swap/test/specialized_in_other.cpp new file mode 100644 index 0000000..7dccab4 --- /dev/null +++ b/swap/test/specialized_in_other.cpp @@ -0,0 +1,72 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace other +namespace other +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace other +namespace other +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + other::swap_test_class object1; + other::swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0); + + return 0; +} +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace other +namespace other +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace other +namespace other +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + other::swap_test_class object1; + other::swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0); + + return 0; +} diff --git a/swap/test/specialized_in_std.cpp b/swap/test/specialized_in_std.cpp new file mode 100644 index 0000000..c6effb0 --- /dev/null +++ b/swap/test/specialized_in_std.cpp @@ -0,0 +1,70 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + + +//Provide swap function in namespace std +namespace std +{ + template <> + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + swap_test_class object1; + swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); + + return 0; +} +// Copyright (c) 2007 Joseph Gauterin +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + + +//Provide swap function in namespace std +namespace std +{ + template <> + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + swap_test_class object1; + swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); + + return 0; +} diff --git a/swap/test/swap_arrays.cpp b/swap/test/swap_arrays.cpp new file mode 100644 index 0000000..f032f5b --- /dev/null +++ b/swap/test/swap_arrays.cpp @@ -0,0 +1,78 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + + +int test_main(int, char*[]) +{ + const std::size_t dimension = 7; + + swap_test_class array1[dimension]; + swap_test_class array2[dimension]; + boost::swap(array1, array2); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); + + swap_test_class::reset(); + + const std::size_t firstDimension = 3; + const std::size_t secondDimension = 4; + + swap_test_class two_d_array1[firstDimension][secondDimension]; + swap_test_class two_d_array2[firstDimension][secondDimension]; + boost::swap(two_d_array1, two_d_array1); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), firstDimension*secondDimension); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); + + return 0; +} +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + + +int test_main(int, char*[]) +{ + const std::size_t dimension = 7; + + swap_test_class array1[dimension]; + swap_test_class array2[dimension]; + boost::swap(array1, array2); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); + + swap_test_class::reset(); + + const std::size_t firstDimension = 3; + const std::size_t secondDimension = 4; + + swap_test_class two_d_array1[firstDimension][secondDimension]; + swap_test_class two_d_array2[firstDimension][secondDimension]; + boost::swap(two_d_array1, two_d_array1); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), firstDimension*secondDimension); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); + + return 0; +} diff --git a/swap/test/swap_test_class.hpp b/swap/test/swap_test_class.hpp new file mode 100644 index 0000000..2b23f20 --- /dev/null +++ b/swap/test/swap_test_class.hpp @@ -0,0 +1,171 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header compiles as a standalone translation unit + +#ifndef BOOST_UTILITY_SWAP_TEST_CLASS_HPP +#define BOOST_UTILITY_SWAP_TEST_CLASS_HPP + + +class swap_test_class +{ +public: + swap_test_class() + { + ++constructCount(); + } + + ~swap_test_class() + { + ++destructCount(); + } + + swap_test_class(const swap_test_class&) + { + ++copyCount(); + ++destructCount(); + } + + swap_test_class& operator=(const swap_test_class&) + { + ++copyCount(); + return *this; + } + + void swap(swap_test_class& other) + { + ++swapCount(); + } + + + static unsigned int swap_count(){ return swapCount(); } + static unsigned int copy_count(){ return copyCount(); } + static unsigned int construct_count(){ return constructCount(); } + static unsigned int destruct_count(){ return destructCount(); } + + static void reset() + { + swapCount() = 0; + copyCount() = 0; + constructCount() = 0; + destructCount() = 0; + } + +private: + static unsigned int& swapCount() + { + static unsigned int value = 0; + return value; + } + + static unsigned int& copyCount() + { + static unsigned int value = 0; + return value; + } + + static unsigned int& constructCount() + { + static unsigned int value = 0; + return value; + } + + static unsigned int& destructCount() + { + static unsigned int value = 0; + return value; + } + +}; + +#endif + +// Copyright (c) 2007 Joseph Gauterin +// +// 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) + +// Tests that the swap header compiles as a standalone translation unit + +#ifndef BOOST_UTILITY_SWAP_TEST_CLASS_HPP +#define BOOST_UTILITY_SWAP_TEST_CLASS_HPP + + +class swap_test_class +{ +public: + swap_test_class() + { + ++constructCount(); + } + + ~swap_test_class() + { + ++destructCount(); + } + + swap_test_class(const swap_test_class&) + { + ++copyCount(); + ++destructCount(); + } + + swap_test_class& operator=(const swap_test_class&) + { + ++copyCount(); + return *this; + } + + void swap(swap_test_class& other) + { + ++swapCount(); + } + + + static unsigned int swap_count(){ return swapCount(); } + static unsigned int copy_count(){ return copyCount(); } + static unsigned int construct_count(){ return constructCount(); } + static unsigned int destruct_count(){ return destructCount(); } + + static void reset() + { + swapCount() = 0; + copyCount() = 0; + constructCount() = 0; + destructCount() = 0; + } + +private: + static unsigned int& swapCount() + { + static unsigned int value = 0; + return value; + } + + static unsigned int& copyCount() + { + static unsigned int value = 0; + return value; + } + + static unsigned int& constructCount() + { + static unsigned int value = 0; + return value; + } + + static unsigned int& destructCount() + { + static unsigned int value = 0; + return value; + } + +}; + +#endif + + From b9dc8d9d4dc23b5f44d18d4eadd9fcee63b64e27 Mon Sep 17 00:00:00 2001 From: Vladimir Prus Date: Sat, 12 Jul 2008 17:56:01 +0000 Subject: [PATCH 06/39] Remove duplicate content. [SVN r47360] --- swap/test/Jamfile.v2 | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 index 758bdaa..a13cbd0 100644 --- a/swap/test/Jamfile.v2 +++ b/swap/test/Jamfile.v2 @@ -22,32 +22,5 @@ test-suite utility/swap [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] ; - - -# Copyright (c) 2007, 2008 Joseph Gauterin -# -# 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) - -# bring in rules for testing -import testing ; - -test-suite utility/swap - : - [ compile root_header_1.cpp ] - [ compile root_header_2.cpp ] - [ compile lib_header_1.cpp ] - [ compile lib_header_2.cpp ] - [ compile mixed_headers_1.cpp ] - [ compile mixed_headers_2.cpp ] - [ run primitive.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run specialized_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] - ; - From b312a3787c017f2b1a26b256498e8837cb37803e Mon Sep 17 00:00:00 2001 From: Joseph Gauterin Date: Sat, 19 Jul 2008 19:40:12 +0000 Subject: [PATCH 07/39] Corrected duplicated file contents [SVN r47607] --- swap/test/lib_header_1.cpp | 11 +--- swap/test/lib_header_2.cpp | 9 --- swap/test/mixed_headers_1.cpp | 9 --- swap/test/mixed_headers_2.cpp | 8 --- swap/test/primitive.cpp | 21 ------- swap/test/root_header_1.cpp | 8 --- swap/test/root_header_2.cpp | 9 --- swap/test/specialized_in_boost.cpp | 35 ----------- swap/test/specialized_in_global.cpp | 29 --------- swap/test/specialized_in_other.cpp | 35 ----------- swap/test/specialized_in_std.cpp | 34 ----------- swap/test/swap_arrays.cpp | 38 ------------ swap/test/swap_test_class.hpp | 91 +---------------------------- 13 files changed, 3 insertions(+), 334 deletions(-) diff --git a/swap/test/lib_header_1.cpp b/swap/test/lib_header_1.cpp index ea7c99a..d3efc63 100644 --- a/swap/test/lib_header_1.cpp +++ b/swap/test/lib_header_1.cpp @@ -6,13 +6,4 @@ // Tests that the swap header compiles as a standalone translation unit -#include -// Copyright (c) 2007 Joseph Gauterin -// -// 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) - -// Tests that the swap header compiles as a standalone translation unit - -#include +#include \ No newline at end of file diff --git a/swap/test/lib_header_2.cpp b/swap/test/lib_header_2.cpp index 0c9e409..e88909d 100644 --- a/swap/test/lib_header_2.cpp +++ b/swap/test/lib_header_2.cpp @@ -8,13 +8,4 @@ #include #include -// Copyright (c) 2007 Joseph Gauterin -// -// 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) -// Tests that the swap header include guards work correctly - -#include -#include diff --git a/swap/test/mixed_headers_1.cpp b/swap/test/mixed_headers_1.cpp index 0c7571e..cdb9fe5 100644 --- a/swap/test/mixed_headers_1.cpp +++ b/swap/test/mixed_headers_1.cpp @@ -8,13 +8,4 @@ #include #include -// Copyright (c) 2007 Joseph Gauterin -// -// 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) -// Tests that the swap headers work when both are included - -#include -#include diff --git a/swap/test/mixed_headers_2.cpp b/swap/test/mixed_headers_2.cpp index 217873a..94e9d87 100644 --- a/swap/test/mixed_headers_2.cpp +++ b/swap/test/mixed_headers_2.cpp @@ -8,13 +8,5 @@ #include #include -// Copyright (c) 2007 Joseph Gauterin -// -// 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) -// Tests that the swap headers work when both are included -#include -#include diff --git a/swap/test/primitive.cpp b/swap/test/primitive.cpp index 76cd7b0..380edb3 100644 --- a/swap/test/primitive.cpp +++ b/swap/test/primitive.cpp @@ -20,25 +20,4 @@ int test_main(int, char*[]) return 0; } -// Copyright (c) 2007 Joseph Gauterin -// -// 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 -#define BOOST_INCLUDE_MAIN -#include - -int test_main(int, char*[]) -{ - int object1 = 1; - int object2 = 2; - - boost::swap(object1,object2); - - BOOST_CHECK_EQUAL(object1,2); - BOOST_CHECK_EQUAL(object2,1); - - return 0; -} diff --git a/swap/test/root_header_1.cpp b/swap/test/root_header_1.cpp index efa0028..575d2cb 100644 --- a/swap/test/root_header_1.cpp +++ b/swap/test/root_header_1.cpp @@ -7,12 +7,4 @@ // Tests that the swap header compiles as a standalone translation unit #include -// Copyright (c) 2007 Joseph Gauterin -// -// 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) -// Tests that the swap header compiles as a standalone translation unit - -#include diff --git a/swap/test/root_header_2.cpp b/swap/test/root_header_2.cpp index d7e00f3..d26b3a6 100644 --- a/swap/test/root_header_2.cpp +++ b/swap/test/root_header_2.cpp @@ -8,13 +8,4 @@ #include #include -// Copyright (c) 2007 Joseph Gauterin -// -// 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) -// Tests that the swap header include guards work correctly - -#include -#include diff --git a/swap/test/specialized_in_boost.cpp b/swap/test/specialized_in_boost.cpp index bc3840f..55ac390 100644 --- a/swap/test/specialized_in_boost.cpp +++ b/swap/test/specialized_in_boost.cpp @@ -34,39 +34,4 @@ int test_main(int, char*[]) return 0; } -// Copyright (c) 2007 Joseph Gauterin -// -// 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 -#define BOOST_INCLUDE_MAIN -#include - -//Put test class in namespace boost -namespace boost -{ - #include "./swap_test_class.hpp" -} - -//Provide swap function in namespace boost -namespace boost -{ - void swap(swap_test_class& left, swap_test_class& right) - { - left.swap(right); - } -} - -int test_main(int, char*[]) -{ - boost::swap_test_class object1; - boost::swap_test_class object2; - boost::swap(object1,object2); - - BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),1); - BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),0); - - return 0; -} diff --git a/swap/test/specialized_in_global.cpp b/swap/test/specialized_in_global.cpp index 7f46187..f76754f 100644 --- a/swap/test/specialized_in_global.cpp +++ b/swap/test/specialized_in_global.cpp @@ -28,33 +28,4 @@ int test_main(int, char*[]) return 0; } -// Copyright (c) 2007 Joseph Gauterin -// -// 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 -#define BOOST_INCLUDE_MAIN -#include - -//Put test class in the global namespace -#include "./swap_test_class.hpp" - -//Provide swap function in gloabl namespace -void swap(swap_test_class& left, swap_test_class& right) -{ - left.swap(right); -} - -int test_main(int, char*[]) -{ - swap_test_class object1; - swap_test_class object2; - boost::swap(object1,object2); - - BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); - BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); - - return 0; -} diff --git a/swap/test/specialized_in_other.cpp b/swap/test/specialized_in_other.cpp index 7dccab4..c3071f5 100644 --- a/swap/test/specialized_in_other.cpp +++ b/swap/test/specialized_in_other.cpp @@ -34,39 +34,4 @@ int test_main(int, char*[]) return 0; } -// Copyright (c) 2007 Joseph Gauterin -// -// 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 -#define BOOST_INCLUDE_MAIN -#include - -//Put test class in namespace other -namespace other -{ - #include "./swap_test_class.hpp" -} - -//Provide swap function in namespace other -namespace other -{ - void swap(swap_test_class& left, swap_test_class& right) - { - left.swap(right); - } -} - -int test_main(int, char*[]) -{ - other::swap_test_class object1; - other::swap_test_class object2; - boost::swap(object1,object2); - - BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1); - BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0); - - return 0; -} diff --git a/swap/test/specialized_in_std.cpp b/swap/test/specialized_in_std.cpp index c6effb0..641aa82 100644 --- a/swap/test/specialized_in_std.cpp +++ b/swap/test/specialized_in_std.cpp @@ -33,38 +33,4 @@ int test_main(int, char*[]) return 0; } -// Copyright (c) 2007 Joseph Gauterin -// -// 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 -#define BOOST_INCLUDE_MAIN -#include - -//Put test class in the global namespace -#include "./swap_test_class.hpp" - - -//Provide swap function in namespace std -namespace std -{ - template <> - void swap(swap_test_class& left, swap_test_class& right) - { - left.swap(right); - } -} - -int test_main(int, char*[]) -{ - swap_test_class object1; - swap_test_class object2; - boost::swap(object1,object2); - - BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); - BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); - - return 0; -} diff --git a/swap/test/swap_arrays.cpp b/swap/test/swap_arrays.cpp index f032f5b..96b7939 100644 --- a/swap/test/swap_arrays.cpp +++ b/swap/test/swap_arrays.cpp @@ -37,42 +37,4 @@ int test_main(int, char*[]) return 0; } -// Copyright (c) 2008 Joseph Gauterin, Niels Dekker -// -// 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 -#define BOOST_INCLUDE_MAIN -#include - -//Put test class in the global namespace -#include "./swap_test_class.hpp" - - -int test_main(int, char*[]) -{ - const std::size_t dimension = 7; - - swap_test_class array1[dimension]; - swap_test_class array2[dimension]; - boost::swap(array1, array2); - - BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); - BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); - - swap_test_class::reset(); - - const std::size_t firstDimension = 3; - const std::size_t secondDimension = 4; - - swap_test_class two_d_array1[firstDimension][secondDimension]; - swap_test_class two_d_array2[firstDimension][secondDimension]; - boost::swap(two_d_array1, two_d_array1); - - BOOST_CHECK_EQUAL(swap_test_class::swap_count(), firstDimension*secondDimension); - BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); - - return 0; -} diff --git a/swap/test/swap_test_class.hpp b/swap/test/swap_test_class.hpp index 2b23f20..aa68444 100644 --- a/swap/test/swap_test_class.hpp +++ b/swap/test/swap_test_class.hpp @@ -1,10 +1,10 @@ -// Copyright (c) 2007 Joseph Gauterin +// Copyright (c) 2007-2008 Joseph Gauterin // // 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) -// Tests that the swap header compiles as a standalone translation unit +// Tests class used by the Boost.Swap tests #ifndef BOOST_UTILITY_SWAP_TEST_CLASS_HPP #define BOOST_UTILITY_SWAP_TEST_CLASS_HPP @@ -82,90 +82,3 @@ private: }; #endif - -// Copyright (c) 2007 Joseph Gauterin -// -// 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) - -// Tests that the swap header compiles as a standalone translation unit - -#ifndef BOOST_UTILITY_SWAP_TEST_CLASS_HPP -#define BOOST_UTILITY_SWAP_TEST_CLASS_HPP - - -class swap_test_class -{ -public: - swap_test_class() - { - ++constructCount(); - } - - ~swap_test_class() - { - ++destructCount(); - } - - swap_test_class(const swap_test_class&) - { - ++copyCount(); - ++destructCount(); - } - - swap_test_class& operator=(const swap_test_class&) - { - ++copyCount(); - return *this; - } - - void swap(swap_test_class& other) - { - ++swapCount(); - } - - - static unsigned int swap_count(){ return swapCount(); } - static unsigned int copy_count(){ return copyCount(); } - static unsigned int construct_count(){ return constructCount(); } - static unsigned int destruct_count(){ return destructCount(); } - - static void reset() - { - swapCount() = 0; - copyCount() = 0; - constructCount() = 0; - destructCount() = 0; - } - -private: - static unsigned int& swapCount() - { - static unsigned int value = 0; - return value; - } - - static unsigned int& copyCount() - { - static unsigned int value = 0; - return value; - } - - static unsigned int& constructCount() - { - static unsigned int value = 0; - return value; - } - - static unsigned int& destructCount() - { - static unsigned int value = 0; - return value; - } - -}; - -#endif - - From 3ef9454c44192ecb1856c94f87ea80c297c30959 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 20 Jul 2008 11:05:49 +0000 Subject: [PATCH 08/39] Fixed silly little bug of mine in swap/test/swap_arrays.cpp [SVN r47626] --- swap/test/swap_arrays.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/test/swap_arrays.cpp b/swap/test/swap_arrays.cpp index 96b7939..6689db7 100644 --- a/swap/test/swap_arrays.cpp +++ b/swap/test/swap_arrays.cpp @@ -30,7 +30,7 @@ int test_main(int, char*[]) swap_test_class two_d_array1[firstDimension][secondDimension]; swap_test_class two_d_array2[firstDimension][secondDimension]; - boost::swap(two_d_array1, two_d_array1); + boost::swap(two_d_array1, two_d_array2); BOOST_CHECK_EQUAL(swap_test_class::swap_count(), firstDimension*secondDimension); BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); From 8696fdaaa6cd0943a2674bf36fd35742710d5f56 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 20 Jul 2008 12:13:33 +0000 Subject: [PATCH 09/39] Added swap_test_class swap functions to test/swap_arrays.cpp. My fault, they should have been there already! [SVN r47628] --- swap/test/swap_arrays.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/swap/test/swap_arrays.cpp b/swap/test/swap_arrays.cpp index 6689db7..154ae30 100644 --- a/swap/test/swap_arrays.cpp +++ b/swap/test/swap_arrays.cpp @@ -11,6 +11,25 @@ //Put test class in the global namespace #include "./swap_test_class.hpp" +//Provide swap function in both the namespace of swap_test_class +//(which is the global namespace), and the std namespace. +//It's common to provide a swap function for a class in both +//namespaces. Scott Meyers recommends doing so: Effectice C++, +//Third Edition, item 25, "Consider support for a non-throwing swap". +void swap(swap_test_class& left, swap_test_class& right) +{ + left.swap(right); +} + +namespace std +{ + template <> + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + int test_main(int, char*[]) { From 8a0f67713f1081c6b19def7779bc1b1dbe00ead3 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 20 Jul 2008 12:18:25 +0000 Subject: [PATCH 10/39] Fixed silly little typo of mine, in test/swap_arrays.cpp [SVN r47629] --- swap/test/swap_arrays.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/test/swap_arrays.cpp b/swap/test/swap_arrays.cpp index 154ae30..79c6d68 100644 --- a/swap/test/swap_arrays.cpp +++ b/swap/test/swap_arrays.cpp @@ -14,7 +14,7 @@ //Provide swap function in both the namespace of swap_test_class //(which is the global namespace), and the std namespace. //It's common to provide a swap function for a class in both -//namespaces. Scott Meyers recommends doing so: Effectice C++, +//namespaces. Scott Meyers recommends doing so: Effective C++, //Third Edition, item 25, "Consider support for a non-throwing swap". void swap(swap_test_class& left, swap_test_class& right) { From 52652243a8e892a2ec7a9c56b3c74548e8e28ed6 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Fri, 25 Jul 2008 18:48:09 +0000 Subject: [PATCH 11/39] Added test_adl_barrier to swap/test, as discussed with Joseph Gauterin. [SVN r47808] --- swap/test/Jamfile.v2 | 5 ++--- swap/test/test_adl_barrier.cpp | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 swap/test/test_adl_barrier.cpp diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 index a13cbd0..d648de2 100644 --- a/swap/test/Jamfile.v2 +++ b/swap/test/Jamfile.v2 @@ -20,7 +20,6 @@ test-suite utility/swap [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run test_adl_barrier.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] ; - - diff --git a/swap/test/test_adl_barrier.cpp b/swap/test/test_adl_barrier.cpp new file mode 100644 index 0000000..e30a1b6 --- /dev/null +++ b/swap/test/test_adl_barrier.cpp @@ -0,0 +1,36 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// boost::swap internally does an unqualified function call to swap. +// This could have led to ambiguity or infinite recursion, when the +// objects to be swapped would themselves be from the boost namespace. +// If so, boost::swap itself might be found by argument dependent lookup +// (ADL). The implementation of boost::swap resolves this issue by +// using a barrier namespace. The following test checks this "ADL barrier". + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace boost +namespace boost +{ + #include "./swap_test_class.hpp" +} + + +int test_main(int, char*[]) +{ + boost::swap_test_class object1; + boost::swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),0); + BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),3); + + return 0; +} + From a6d33f622d30c309d35fa55803a283aabc0a7a7c Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sat, 26 Jul 2008 17:47:59 +0000 Subject: [PATCH 12/39] Added a newline to swap/test/lib_header_1.cpp, hoping to fix Sun 5.x compile issue, "Error: There is extra text on this line" [SVN r47829] --- swap/test/lib_header_1.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/swap/test/lib_header_1.cpp b/swap/test/lib_header_1.cpp index d3efc63..923dea6 100644 --- a/swap/test/lib_header_1.cpp +++ b/swap/test/lib_header_1.cpp @@ -6,4 +6,5 @@ // Tests that the swap header compiles as a standalone translation unit -#include \ No newline at end of file +#include + From 969fbda17a734ba35ec5249181969ffa926d0780 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 27 Jul 2008 11:35:33 +0000 Subject: [PATCH 13/39] Added specialized_in_boost_and_other to swap/test, as discussed at "[boost] [swap] End-user allowed to add overloads to boost namespace?", http://lists.boost.org/Archives/boost/2008/07/140327.php [SVN r47839] --- swap/test/Jamfile.v2 | 15 +++--- swap/test/specialized_in_boost_and_other.cpp | 56 ++++++++++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 swap/test/specialized_in_boost_and_other.cpp diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 index d648de2..abce519 100644 --- a/swap/test/Jamfile.v2 +++ b/swap/test/Jamfile.v2 @@ -15,11 +15,12 @@ test-suite utility/swap [ compile lib_header_2.cpp ] [ compile mixed_headers_1.cpp ] [ compile mixed_headers_2.cpp ] - [ run primitive.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run specialized_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run test_adl_barrier.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run primitive.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_boost_and_other.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run test_adl_barrier.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] ; diff --git a/swap/test/specialized_in_boost_and_other.cpp b/swap/test/specialized_in_boost_and_other.cpp new file mode 100644 index 0000000..fc76251 --- /dev/null +++ b/swap/test/specialized_in_boost_and_other.cpp @@ -0,0 +1,56 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// Tests whether two object in a namespace other than boost are properly +// swapped, when both boost and the other namespace have a custom swap function +// for their class. Note that it shouldn't be necessary for a class in an other +// namespace to have a custom swap function in boost, because the boost::swap +// utility should find the swap function in the other namespace, by argument +// dependent lookup (ADL). Unfortunately ADL isn't fully implemented by some +// specific compilers, including Intel C++ 8.1, MSVC 7.1, and Borland 5.9.3. +// Users of those compilers might consider adding a swap overload to the boost +// namespace. + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace other +namespace other +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace boost +namespace boost +{ + void swap(::other::swap_test_class& left, ::other::swap_test_class& right) + { + left.swap(right); + } +} + +//Provide swap function in namespace other +namespace other +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + other::swap_test_class object1; + other::swap_test_class object2; + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0); + + return 0; +} + From 9b76f1f12089786c29f1419a6bf0ac4880155dd5 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 27 Jul 2008 12:46:45 +0000 Subject: [PATCH 14/39] Fixed comment in swap/test/specialized_in_boost_and_other.cpp [SVN r47840] --- swap/test/specialized_in_boost_and_other.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/swap/test/specialized_in_boost_and_other.cpp b/swap/test/specialized_in_boost_and_other.cpp index fc76251..9adc5c0 100644 --- a/swap/test/specialized_in_boost_and_other.cpp +++ b/swap/test/specialized_in_boost_and_other.cpp @@ -4,15 +4,15 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -// Tests whether two object in a namespace other than boost are properly -// swapped, when both boost and the other namespace have a custom swap function -// for their class. Note that it shouldn't be necessary for a class in an other -// namespace to have a custom swap function in boost, because the boost::swap -// utility should find the swap function in the other namespace, by argument -// dependent lookup (ADL). Unfortunately ADL isn't fully implemented by some -// specific compilers, including Intel C++ 8.1, MSVC 7.1, and Borland 5.9.3. -// Users of those compilers might consider adding a swap overload to the boost -// namespace. +// Tests whether instances of a class from a namespace other than boost are +// properly swapped, when both boost and the other namespace have a custom +// swap function for that class. Note that it shouldn't be necessary for a class +// in an other namespace to have a custom swap function in boost, because the +// boost::swap utility should find the swap function in the other namespace, by +// argument dependent lookup (ADL). Unfortunately ADL isn't fully implemented +// by some specific compiler versions, including Intel C++ 8.1, MSVC 7.1, and +// Borland 5.9.3. Users of those compilers might consider adding a swap overload +// to the boost namespace. #include #define BOOST_INCLUDE_MAIN From a4ca52777077b8f6e34312c55872dcbef56909d8 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Wed, 30 Jul 2008 08:04:34 +0000 Subject: [PATCH 15/39] Applied "swap.hpp.patch" by Steven Watanabe, "Re: [boost] [swap] How to fix ADL barrier for XL, Intel, GCC, Sun and Como?", http://lists.boost.org/Archives/boost/2008/07/140482.php [SVN r47877] --- include/boost/utility/swap.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/utility/swap.hpp b/include/boost/utility/swap.hpp index 952adf5..16750c8 100644 --- a/include/boost/utility/swap.hpp +++ b/include/boost/utility/swap.hpp @@ -38,8 +38,8 @@ namespace boost { namespace swap_adl_barrier { - template - void swap(T& left, T& right) + template + void swap(T1& left, T2& right) { ::boost_swap_impl::swap_impl(left, right); } From e94718427e46398e2c1cf1c42cd0ef4cc5dc7f7c Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Thu, 31 Jul 2008 20:18:04 +0000 Subject: [PATCH 16/39] Removed swap_adl_barrier namespace, as discussed at "Re: [boost] [swap] How to fix ADL barrier for XL, Intel, GCC, Sun and Como?", http://lists.boost.org/Archives/boost/2008/07/140511.php [SVN r47920] --- include/boost/utility/swap.hpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/include/boost/utility/swap.hpp b/include/boost/utility/swap.hpp index 16750c8..da293ee 100644 --- a/include/boost/utility/swap.hpp +++ b/include/boost/utility/swap.hpp @@ -36,16 +36,11 @@ namespace boost_swap_impl namespace boost { - namespace swap_adl_barrier + template + void swap(T1& left, T2& right) { - template - void swap(T1& left, T2& right) - { - ::boost_swap_impl::swap_impl(left, right); - } + ::boost_swap_impl::swap_impl(left, right); } - - using swap_adl_barrier::swap; } #endif From 19ddea375bfdc4eec2827a901ded6b8abee3c24c Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sat, 2 Aug 2008 11:41:47 +0000 Subject: [PATCH 17/39] Added swap tests for std types, as discussed at "Re: [boost] [swap] Workaround for ADL failures of MSVC 7.1 and Borland okay?", http://lists.boost.org/Archives/boost/2008/08/140589.php [SVN r47943] --- swap/test/Jamfile.v2 | 7 ++++ swap/test/std_bitset.cpp | 33 ++++++++++++++++++ swap/test/std_dateorder.cpp | 32 ++++++++++++++++++ swap/test/std_string.cpp | 31 +++++++++++++++++ swap/test/std_typeinfo_ptr.cpp | 32 ++++++++++++++++++ swap/test/std_vector_of_boost.cpp | 54 ++++++++++++++++++++++++++++++ swap/test/std_vector_of_global.cpp | 47 ++++++++++++++++++++++++++ swap/test/std_vector_of_other.cpp | 54 ++++++++++++++++++++++++++++++ 8 files changed, 290 insertions(+) create mode 100644 swap/test/std_bitset.cpp create mode 100644 swap/test/std_dateorder.cpp create mode 100644 swap/test/std_string.cpp create mode 100644 swap/test/std_typeinfo_ptr.cpp create mode 100644 swap/test/std_vector_of_boost.cpp create mode 100644 swap/test/std_vector_of_global.cpp create mode 100644 swap/test/std_vector_of_other.cpp diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 index abce519..d08a47a 100644 --- a/swap/test/Jamfile.v2 +++ b/swap/test/Jamfile.v2 @@ -21,6 +21,13 @@ test-suite utility/swap [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run specialized_in_boost_and_other.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_bitset.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_dateorder.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_string.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_typeinfo_ptr.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_vector_of_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_vector_of_global.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_vector_of_other.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run test_adl_barrier.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] ; diff --git a/swap/test/std_bitset.cpp b/swap/test/std_bitset.cpp new file mode 100644 index 0000000..4b9fbae --- /dev/null +++ b/swap/test/std_bitset.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// Tests swapping std::bitset objects by means of boost::swap. +// Unlike most other Standard C++ Library template classes, +// std::bitset does not have its own std::swap overload. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +int test_main(int, char*[]) +{ + typedef std::bitset<8> bitset_type; + const bitset_type initial_value1 = 1ul; + const bitset_type initial_value2 = 2ul; + + bitset_type object1 = initial_value1; + bitset_type object2 = initial_value2; + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1,initial_value2); + BOOST_CHECK_EQUAL(object2,initial_value1); + + return 0; +} + diff --git a/swap/test/std_dateorder.cpp b/swap/test/std_dateorder.cpp new file mode 100644 index 0000000..b593f6f --- /dev/null +++ b/swap/test/std_dateorder.cpp @@ -0,0 +1,32 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// Tests swapping std::time_base::dateorder objects by means of boost::swap. +// std::time_base::dateorder is an enumerated type. It does not have an +// std::swap overload or template specialization. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +int test_main(int, char*[]) +{ + const std::time_base::dateorder initial_value1 = std::time_base::dmy; + const std::time_base::dateorder initial_value2 = std::time_base::mdy; + + std::time_base::dateorder object1 = initial_value1; + std::time_base::dateorder object2 = initial_value2; + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1,initial_value2); + BOOST_CHECK_EQUAL(object2,initial_value1); + + return 0; +} + diff --git a/swap/test/std_string.cpp b/swap/test/std_string.cpp new file mode 100644 index 0000000..b7d3d4d --- /dev/null +++ b/swap/test/std_string.cpp @@ -0,0 +1,31 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// Tests swapping std::string objects by means of boost::swap. +// std::string has its own std::swap overload. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +int test_main(int, char*[]) +{ + const std::string initial_value1 = "one"; + const std::string initial_value2 = "two"; + + std::string object1 = initial_value1; + std::string object2 = initial_value2; + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1,initial_value2); + BOOST_CHECK_EQUAL(object2,initial_value1); + + return 0; +} + diff --git a/swap/test/std_typeinfo_ptr.cpp b/swap/test/std_typeinfo_ptr.cpp new file mode 100644 index 0000000..38e293a --- /dev/null +++ b/swap/test/std_typeinfo_ptr.cpp @@ -0,0 +1,32 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// Tests swapping std::type_info pointers by means of boost::swap. +// There is no std::swap overload or template specialization +// for std::type_info pointers. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +int test_main(int, char*[]) +{ + const std::type_info * const initial_value1 = 0; + const std::type_info * const initial_value2 = &typeid(double); + + const std::type_info * ptr1 = initial_value1; + const std::type_info * ptr2 = initial_value2; + + boost::swap(ptr1,ptr2); + + BOOST_CHECK_EQUAL(ptr1,initial_value2); + BOOST_CHECK_EQUAL(ptr2,initial_value1); + + return 0; +} + diff --git a/swap/test/std_vector_of_boost.cpp b/swap/test/std_vector_of_boost.cpp new file mode 100644 index 0000000..f9c3888 --- /dev/null +++ b/swap/test/std_vector_of_boost.cpp @@ -0,0 +1,54 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// Tests swapping std::vector objects by means of boost::swap, +// having boost::swap_test_class as vector element type. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +//Put test class in namespace boost +namespace boost +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace boost +namespace boost +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + typedef boost::swap_test_class swap_test_class_type; + typedef std::vector vector_type; + + const vector_type::size_type initial_size1 = 1; + const vector_type::size_type initial_size2 = 2; + + vector_type object1(initial_size1); + vector_type object2(initial_size2); + + swap_test_class_type::reset(); + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1.size(),initial_size2); + BOOST_CHECK_EQUAL(object2.size(),initial_size1); + + BOOST_CHECK_EQUAL(swap_test_class_type::swap_count(),0); + BOOST_CHECK_EQUAL(swap_test_class_type::copy_count(),0); + + return 0; +} + diff --git a/swap/test/std_vector_of_global.cpp b/swap/test/std_vector_of_global.cpp new file mode 100644 index 0000000..ef6b52b --- /dev/null +++ b/swap/test/std_vector_of_global.cpp @@ -0,0 +1,47 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// Tests swapping std::vector objects by means of boost::swap, +// having ::swap_test_class as vector element type. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + +//Provide swap function in the global namespace +void swap(swap_test_class& left, swap_test_class& right) +{ + left.swap(right); +} + +int test_main(int, char*[]) +{ + typedef std::vector vector_type; + + const vector_type::size_type initial_size1 = 1; + const vector_type::size_type initial_size2 = 2; + + vector_type object1(initial_size1); + vector_type object2(initial_size2); + + swap_test_class::reset(); + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1.size(),initial_size2); + BOOST_CHECK_EQUAL(object2.size(),initial_size1); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),0); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); + + return 0; +} + diff --git a/swap/test/std_vector_of_other.cpp b/swap/test/std_vector_of_other.cpp new file mode 100644 index 0000000..3834269 --- /dev/null +++ b/swap/test/std_vector_of_other.cpp @@ -0,0 +1,54 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// Tests swapping std::vector objects by means of boost::swap, +// having other::swap_test_class as vector element type. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +//Put test class in namespace other +namespace other +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace other +namespace other +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + typedef other::swap_test_class swap_test_class_type; + typedef std::vector vector_type; + + const vector_type::size_type initial_size1 = 1; + const vector_type::size_type initial_size2 = 2; + + vector_type object1(initial_size1); + vector_type object2(initial_size2); + + swap_test_class_type::reset(); + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1.size(),initial_size2); + BOOST_CHECK_EQUAL(object2.size(),initial_size1); + + BOOST_CHECK_EQUAL(swap_test_class_type::swap_count(),0); + BOOST_CHECK_EQUAL(swap_test_class_type::copy_count(),0); + + return 0; +} + From f05e7b2785f368493ec0d3cdb3efac23417652f0 Mon Sep 17 00:00:00 2001 From: Joseph Gauterin Date: Mon, 4 Aug 2008 11:21:02 +0000 Subject: [PATCH 18/39] Changed 'using std::swap;' to 'using namesapce std;' in swap_impl function to work around ADL bugs in some compilers. [SVN r47967] --- include/boost/utility/swap.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/utility/swap.hpp b/include/boost/utility/swap.hpp index da293ee..71cc454 100644 --- a/include/boost/utility/swap.hpp +++ b/include/boost/utility/swap.hpp @@ -20,7 +20,7 @@ namespace boost_swap_impl template void swap_impl(T& left, T& right) { - using std::swap;//use std::swap if argument dependent lookup fails + using namespace std;//use std::swap if argument dependent lookup fails swap(left,right); } From 7175e024249ef82079898b780fcb433f0e255363 Mon Sep 17 00:00:00 2001 From: Joseph Gauterin Date: Mon, 4 Aug 2008 18:16:16 +0000 Subject: [PATCH 19/39] Updated copyright info. [SVN r47971] --- include/boost/utility/swap.hpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/include/boost/utility/swap.hpp b/include/boost/utility/swap.hpp index 71cc454..f42264c 100644 --- a/include/boost/utility/swap.hpp +++ b/include/boost/utility/swap.hpp @@ -1,12 +1,9 @@ -// Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin +// Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin, Niels Dekker // // 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) // For more information, see http://www.boost.org -// -// Update: -// 29 June 2008 (Added support for built-in arrays.) Niels Dekker #ifndef BOOST_UTILITY_SWAP_HPP From 323bb9dcb1493ca03dda08eb1093fa9fc3c09427 Mon Sep 17 00:00:00 2001 From: Joseph Gauterin Date: Mon, 4 Aug 2008 18:22:10 +0000 Subject: [PATCH 20/39] Renamed 'test_adl_barrier.cpp' to 'no_ambiguity_in_boost.cpp' and altered comments to reflect new disambiguation technique. [SVN r47972] --- swap/test/Jamfile.v2 | 2 +- .../{test_adl_barrier.cpp => no_ambiguity_in_boost.cpp} | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) rename swap/test/{test_adl_barrier.cpp => no_ambiguity_in_boost.cpp} (84%) diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 index d08a47a..7071837 100644 --- a/swap/test/Jamfile.v2 +++ b/swap/test/Jamfile.v2 @@ -28,6 +28,6 @@ test-suite utility/swap [ run std_vector_of_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run std_vector_of_global.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run std_vector_of_other.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run test_adl_barrier.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run no_ambiguity_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] ; diff --git a/swap/test/test_adl_barrier.cpp b/swap/test/no_ambiguity_in_boost.cpp similarity index 84% rename from swap/test/test_adl_barrier.cpp rename to swap/test/no_ambiguity_in_boost.cpp index e30a1b6..17fe9d6 100644 --- a/swap/test/test_adl_barrier.cpp +++ b/swap/test/no_ambiguity_in_boost.cpp @@ -7,9 +7,10 @@ // boost::swap internally does an unqualified function call to swap. // This could have led to ambiguity or infinite recursion, when the // objects to be swapped would themselves be from the boost namespace. -// If so, boost::swap itself might be found by argument dependent lookup -// (ADL). The implementation of boost::swap resolves this issue by -// using a barrier namespace. The following test checks this "ADL barrier". +// If so, boost::swap itself might be found by argument dependent lookup. +// The implementation of boost::swap resolves this issue by giving +// boost::swap two template argumetns, thereby making it less specialized +// than std::swap. #include #define BOOST_INCLUDE_MAIN From 3264868197ae8b9b30a510874b1c89d3f101978c Mon Sep 17 00:00:00 2001 From: Joseph Gauterin Date: Mon, 4 Aug 2008 18:25:45 +0000 Subject: [PATCH 21/39] Updated documentation to remove references to the 'ADL barrier' [SVN r47973] --- swap.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/swap.html b/swap.html index e317b11..dfe9b84 100644 --- a/swap.html +++ b/swap.html @@ -1,4 +1,4 @@ -? + @@ -73,7 +73,7 @@ Joseph Gauterin - for the initial idea, implementation, tests, and documentation
  • - Steven Wanatabe - for the idea to use a barrier namespace, enabling the function to have the name 'swap' without introducing ambiguity or infinite recursion + Steven Wanatabe - for the idea to make boost::swap less specialized than std::swap, thereby allowing the function to have the name 'swap' without introducing ambiguity
  • @@ -84,7 +84,7 @@
    -

    Revised: 3 July 2008

    +

    Revised: 4 August 2008

    Copyright 2007, 2008 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.) From 1d893172d0c9b238d4a9781de36062375029bd07 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Wed, 13 Aug 2008 22:00:35 +0000 Subject: [PATCH 22/39] Fix Windows-1252 dash in UTF-8 document. [SVN r48133] --- swap.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap.html b/swap.html index dfe9b84..2af5a88 100644 --- a/swap.html +++ b/swap.html @@ -60,7 +60,7 @@

    Portability

    - Several older compilers do not support argument dependent lookup – on these compilers boost::swap will call std::swap, ignoring any specialized swap functions that could be found as a result of argument dependent lookup. + Several older compilers do not support argument dependent lookup ‒ on these compilers boost::swap will call std::swap, ignoring any specialized swap functions that could be found as a result of argument dependent lookup.

    From bf03fdf92480dc04645819cd09c4462463197485 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sat, 16 Aug 2008 08:56:19 +0000 Subject: [PATCH 23/39] Added explanatory comments, requested by Isaac Dupree, "Re: [boost] [swap] Renaming boost_swap_impl::swap_impl and/or its namespace?", http://lists.boost.org/Archives/boost/2008/08/141007.php [SVN r48171] --- include/boost/utility/swap.hpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/boost/utility/swap.hpp b/include/boost/utility/swap.hpp index f42264c..6845e79 100644 --- a/include/boost/utility/swap.hpp +++ b/include/boost/utility/swap.hpp @@ -9,6 +9,18 @@ #ifndef BOOST_UTILITY_SWAP_HPP #define BOOST_UTILITY_SWAP_HPP +// Note: the implementation of this utility contains various workarounds: +// - swap_impl is put outside the boost namespace, to avoid infinite +// recursion (causing stack overflow) when swapping objects of a primitive +// type. +// - swap_impl has a using-directive, rather than a using-declaration, +// because some compilers (including MSVC 7.1, Borland 5.9.3, and +// Intel 8.1) don't do argument-dependent lookup when it has a +// using-declaration instead. +// - boost::swap has two template arguments, instead of one, to +// avoid ambiguity when swapping objects of a Boost type that does +// not have its own boost::swap overload. + #include //for std::swap #include //for std::size_t From 28ed4e0d67c579f6f2a5e3923bf7619ec0f7bc0c Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Wed, 20 Aug 2008 08:25:23 +0000 Subject: [PATCH 24/39] Added a data member to swap_test_class and made it EqualityComparable, as I mentioned at "Re: [boost] [swap] Renaming boost_swap_impl::swap_impl and/or its namespace?", http://lists.boost.org/Archives/boost/2008/08/141027.php [SVN r48245] --- swap/test/swap_test_class.hpp | 36 ++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/swap/test/swap_test_class.hpp b/swap/test/swap_test_class.hpp index aa68444..8cf1fe9 100644 --- a/swap/test/swap_test_class.hpp +++ b/swap/test/swap_test_class.hpp @@ -12,8 +12,11 @@ class swap_test_class { + int m_data; public: - swap_test_class() + explicit swap_test_class(int arg = 0) + : + m_data(arg) { ++constructCount(); } @@ -23,24 +26,40 @@ public: ++destructCount(); } - swap_test_class(const swap_test_class&) + swap_test_class(const swap_test_class& arg) + : + m_data(arg.m_data) { ++copyCount(); ++destructCount(); } - swap_test_class& operator=(const swap_test_class&) + swap_test_class& operator=(const swap_test_class& arg) { + m_data = arg.m_data; ++copyCount(); return *this; } void swap(swap_test_class& other) { + const int temp = m_data; + m_data = other.m_data; + other.m_data = temp; + ++swapCount(); } + int get_data() const + { + return m_data; + } + void set_data(int arg) + { + m_data = arg; + } + static unsigned int swap_count(){ return swapCount(); } static unsigned int copy_count(){ return copyCount(); } static unsigned int construct_count(){ return constructCount(); } @@ -81,4 +100,15 @@ private: }; + +inline bool operator==(const swap_test_class & lhs, const swap_test_class & rhs) +{ + return lhs.get_data() == rhs.get_data(); +} + +inline bool operator!=(const swap_test_class & lhs, const swap_test_class & rhs) +{ + return !(lhs == rhs); +} + #endif From 622f775a7ff9ee4445a551af4b0abb5f92876d24 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Wed, 20 Aug 2008 08:28:35 +0000 Subject: [PATCH 25/39] Added extra checks, checking that boost::swap does correctly exchange the values of its arguments, as I mentioned at "Re: [boost] [swap] Renaming boost_swap_impl::swap_impl and/or its namespace?", http://lists.boost.org/Archives/boost/2008/08/141027.php [SVN r48246] --- swap/test/no_ambiguity_in_boost.cpp | 11 +++++++++-- swap/test/specialized_in_boost.cpp | 12 ++++++++++-- swap/test/specialized_in_boost_and_other.cpp | 12 ++++++++++-- swap/test/specialized_in_global.cpp | 12 ++++++++++-- swap/test/specialized_in_other.cpp | 12 ++++++++++-- swap/test/specialized_in_std.cpp | 12 ++++++++++-- swap/test/std_vector_of_boost.cpp | 10 ++++++++-- swap/test/std_vector_of_global.cpp | 10 ++++++++-- swap/test/std_vector_of_other.cpp | 10 ++++++++-- 9 files changed, 83 insertions(+), 18 deletions(-) diff --git a/swap/test/no_ambiguity_in_boost.cpp b/swap/test/no_ambiguity_in_boost.cpp index 17fe9d6..e269252 100644 --- a/swap/test/no_ambiguity_in_boost.cpp +++ b/swap/test/no_ambiguity_in_boost.cpp @@ -25,10 +25,17 @@ namespace boost int test_main(int, char*[]) { - boost::swap_test_class object1; - boost::swap_test_class object2; + const boost::swap_test_class initial_value1(1); + const boost::swap_test_class initial_value2(2); + + boost::swap_test_class object1 = initial_value1; + boost::swap_test_class object2 = initial_value2; + + boost::swap_test_class::reset(); boost::swap(object1,object2); + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),0); BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),3); diff --git a/swap/test/specialized_in_boost.cpp b/swap/test/specialized_in_boost.cpp index 55ac390..7b3e12e 100644 --- a/swap/test/specialized_in_boost.cpp +++ b/swap/test/specialized_in_boost.cpp @@ -25,10 +25,18 @@ namespace boost int test_main(int, char*[]) { - boost::swap_test_class object1; - boost::swap_test_class object2; + const boost::swap_test_class initial_value1(1); + const boost::swap_test_class initial_value2(2); + + boost::swap_test_class object1 = initial_value1; + boost::swap_test_class object2 = initial_value2; + + boost::swap_test_class::reset(); boost::swap(object1,object2); + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),1); BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),0); diff --git a/swap/test/specialized_in_boost_and_other.cpp b/swap/test/specialized_in_boost_and_other.cpp index 9adc5c0..ba6247f 100644 --- a/swap/test/specialized_in_boost_and_other.cpp +++ b/swap/test/specialized_in_boost_and_other.cpp @@ -44,10 +44,18 @@ namespace other int test_main(int, char*[]) { - other::swap_test_class object1; - other::swap_test_class object2; + const other::swap_test_class initial_value1(1); + const other::swap_test_class initial_value2(2); + + other::swap_test_class object1 = initial_value1; + other::swap_test_class object2 = initial_value2; + + other::swap_test_class::reset(); boost::swap(object1,object2); + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1); BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0); diff --git a/swap/test/specialized_in_global.cpp b/swap/test/specialized_in_global.cpp index f76754f..0a0a029 100644 --- a/swap/test/specialized_in_global.cpp +++ b/swap/test/specialized_in_global.cpp @@ -19,10 +19,18 @@ void swap(swap_test_class& left, swap_test_class& right) int test_main(int, char*[]) { - swap_test_class object1; - swap_test_class object2; + const swap_test_class initial_value1(1); + const swap_test_class initial_value2(2); + + swap_test_class object1 = initial_value1; + swap_test_class object2 = initial_value2; + + swap_test_class::reset(); boost::swap(object1,object2); + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); diff --git a/swap/test/specialized_in_other.cpp b/swap/test/specialized_in_other.cpp index c3071f5..a39896f 100644 --- a/swap/test/specialized_in_other.cpp +++ b/swap/test/specialized_in_other.cpp @@ -25,10 +25,18 @@ namespace other int test_main(int, char*[]) { - other::swap_test_class object1; - other::swap_test_class object2; + const other::swap_test_class initial_value1(1); + const other::swap_test_class initial_value2(2); + + other::swap_test_class object1 = initial_value1; + other::swap_test_class object2 = initial_value2; + + other::swap_test_class::reset(); boost::swap(object1,object2); + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1); BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0); diff --git a/swap/test/specialized_in_std.cpp b/swap/test/specialized_in_std.cpp index 641aa82..e31e850 100644 --- a/swap/test/specialized_in_std.cpp +++ b/swap/test/specialized_in_std.cpp @@ -24,10 +24,18 @@ namespace std int test_main(int, char*[]) { - swap_test_class object1; - swap_test_class object2; + const swap_test_class initial_value1(1); + const swap_test_class initial_value2(2); + + swap_test_class object1 = initial_value1; + swap_test_class object2 = initial_value2; + + swap_test_class::reset(); boost::swap(object1,object2); + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); diff --git a/swap/test/std_vector_of_boost.cpp b/swap/test/std_vector_of_boost.cpp index f9c3888..b0c6355 100644 --- a/swap/test/std_vector_of_boost.cpp +++ b/swap/test/std_vector_of_boost.cpp @@ -36,8 +36,11 @@ int test_main(int, char*[]) const vector_type::size_type initial_size1 = 1; const vector_type::size_type initial_size2 = 2; - vector_type object1(initial_size1); - vector_type object2(initial_size2); + const vector_type initial_value1(initial_size1, swap_test_class_type(1)); + const vector_type initial_value2(initial_size2, swap_test_class_type(2)); + + vector_type object1 = initial_value1; + vector_type object2 = initial_value2; swap_test_class_type::reset(); @@ -46,6 +49,9 @@ int test_main(int, char*[]) BOOST_CHECK_EQUAL(object1.size(),initial_size2); BOOST_CHECK_EQUAL(object2.size(),initial_size1); + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + BOOST_CHECK_EQUAL(swap_test_class_type::swap_count(),0); BOOST_CHECK_EQUAL(swap_test_class_type::copy_count(),0); diff --git a/swap/test/std_vector_of_global.cpp b/swap/test/std_vector_of_global.cpp index ef6b52b..96a9b6a 100644 --- a/swap/test/std_vector_of_global.cpp +++ b/swap/test/std_vector_of_global.cpp @@ -29,8 +29,11 @@ int test_main(int, char*[]) const vector_type::size_type initial_size1 = 1; const vector_type::size_type initial_size2 = 2; - vector_type object1(initial_size1); - vector_type object2(initial_size2); + const vector_type initial_value1(initial_size1, swap_test_class(1)); + const vector_type initial_value2(initial_size2, swap_test_class(2)); + + vector_type object1 = initial_value1; + vector_type object2 = initial_value2; swap_test_class::reset(); @@ -39,6 +42,9 @@ int test_main(int, char*[]) BOOST_CHECK_EQUAL(object1.size(),initial_size2); BOOST_CHECK_EQUAL(object2.size(),initial_size1); + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),0); BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); diff --git a/swap/test/std_vector_of_other.cpp b/swap/test/std_vector_of_other.cpp index 3834269..2176f6e 100644 --- a/swap/test/std_vector_of_other.cpp +++ b/swap/test/std_vector_of_other.cpp @@ -36,8 +36,11 @@ int test_main(int, char*[]) const vector_type::size_type initial_size1 = 1; const vector_type::size_type initial_size2 = 2; - vector_type object1(initial_size1); - vector_type object2(initial_size2); + const vector_type initial_value1(initial_size1, swap_test_class_type(1)); + const vector_type initial_value2(initial_size2, swap_test_class_type(2)); + + vector_type object1 = initial_value1; + vector_type object2 = initial_value2; swap_test_class_type::reset(); @@ -46,6 +49,9 @@ int test_main(int, char*[]) BOOST_CHECK_EQUAL(object1.size(),initial_size2); BOOST_CHECK_EQUAL(object2.size(),initial_size1); + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + BOOST_CHECK_EQUAL(swap_test_class_type::swap_count(),0); BOOST_CHECK_EQUAL(swap_test_class_type::copy_count(),0); From 72562af7af8be2f2778f0a7f8277d7f776e4872c Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Wed, 20 Aug 2008 08:29:54 +0000 Subject: [PATCH 26/39] Extended swap_arrays test, checking that boost::swap does correctly exchange the values of its arguments. [SVN r48247] --- swap/test/swap_arrays.cpp | 63 ++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/swap/test/swap_arrays.cpp b/swap/test/swap_arrays.cpp index 79c6d68..eeb64bc 100644 --- a/swap/test/swap_arrays.cpp +++ b/swap/test/swap_arrays.cpp @@ -11,6 +11,9 @@ //Put test class in the global namespace #include "./swap_test_class.hpp" +#include //for std::copy and std::equal +#include //for std::size_t + //Provide swap function in both the namespace of swap_test_class //(which is the global namespace), and the std namespace. //It's common to provide a swap function for a class in both @@ -30,29 +33,67 @@ namespace std } } - -int test_main(int, char*[]) +// Tests swapping 1-dimensional arrays. +void test_swapping_1D_arrays() { - const std::size_t dimension = 7; - + const std::size_t dimension = 2; + const swap_test_class initial_array1[dimension] = { swap_test_class(1), swap_test_class(2) }; + const swap_test_class initial_array2[dimension] = { swap_test_class(3), swap_test_class(4) }; + swap_test_class array1[dimension]; swap_test_class array2[dimension]; + + std::copy(initial_array1, initial_array1 + dimension, array1); + std::copy(initial_array2, initial_array2 + dimension, array2); + + swap_test_class::reset(); boost::swap(array1, array2); + BOOST_CHECK(std::equal(array1, array1 + dimension, initial_array2)); + BOOST_CHECK(std::equal(array2, array2 + dimension, initial_array1)); + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); +} + + +// Tests swapping 2-dimensional arrays. +void test_swapping_2D_arrays() +{ + const std::size_t first_dimension = 3; + const std::size_t second_dimension = 4; + const std::size_t number_of_elements = first_dimension * second_dimension; + + swap_test_class array1[first_dimension][second_dimension]; + swap_test_class array2[first_dimension][second_dimension]; + + swap_test_class* const ptr1 = array1[0]; + swap_test_class* const ptr2 = array2[0]; + + for (std::size_t i = 0; i < number_of_elements; ++i) + { + ptr1[i].set_data( static_cast(i) ); + ptr2[i].set_data( static_cast(i + number_of_elements) ); + } swap_test_class::reset(); + boost::swap(array1, array2); - const std::size_t firstDimension = 3; - const std::size_t secondDimension = 4; + for (std::size_t i = 0; i < number_of_elements; ++i) + { + BOOST_CHECK_EQUAL(ptr1[i].get_data(), static_cast(i + number_of_elements) ); + BOOST_CHECK_EQUAL(ptr2[i].get_data(), static_cast(i) ); + } - swap_test_class two_d_array1[firstDimension][secondDimension]; - swap_test_class two_d_array2[firstDimension][secondDimension]; - boost::swap(two_d_array1, two_d_array2); - - BOOST_CHECK_EQUAL(swap_test_class::swap_count(), firstDimension*secondDimension); + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), number_of_elements); BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); +} + + +int test_main(int, char*[]) +{ + test_swapping_1D_arrays(); + test_swapping_2D_arrays(); return 0; } From 8a738dcef384beb322ce176263c7b3acc0d595ce Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Tue, 21 Oct 2008 09:55:54 +0000 Subject: [PATCH 27/39] Swap documentation: fixed a misspelling of the name of Steven Watanabe. [SVN r49416] --- swap.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swap.html b/swap.html index 2af5a88..01afd7c 100644 --- a/swap.html +++ b/swap.html @@ -73,7 +73,7 @@ Joseph Gauterin - for the initial idea, implementation, tests, and documentation
  • - Steven Wanatabe - for the idea to make boost::swap less specialized than std::swap, thereby allowing the function to have the name 'swap' without introducing ambiguity + Steven Watanabe - for the idea to make boost::swap less specialized than std::swap, thereby allowing the function to have the name 'swap' without introducing ambiguity
  • @@ -84,7 +84,7 @@
    -

    Revised: 4 August 2008

    +

    Revised: 21 October 2008

    Copyright 2007, 2008 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.) From f0276eac7d1142ff5fd986582e85b382db38f773 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sat, 15 Nov 2008 15:07:42 +0000 Subject: [PATCH 28/39] Updated swap.html because LWG issue 809 is now accepted as a defect. Fixed some HTML formatting. [SVN r49771] --- swap.html | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/swap.html b/swap.html index 01afd7c..4022e0b 100644 --- a/swap.html +++ b/swap.html @@ -14,7 +14,7 @@ template<class T> void swap(T& left, T& right);

    - +

    The template function boost::swap allows the values of two variables to be swapped, using argument dependent lookup to select a specialized swap function if available. If no specialized swap function is available, std::swap is used.

    @@ -24,10 +24,10 @@

    The generic std::swap function requires that the elements to be swapped are assignable and copy constructible. It is usually implemented using one copy construction and two assignments - this is often both unnecessarily restrictive and unnecessarily slow. In addition, where the generic swap implementation provides only the basic guarantee, specialized swap functions are often able to provide the no-throw exception guarantee (and it is considered best practice to do so where possible1).

    - The alternative to using argument dependent lookup in this situation is to provide a template specialization of std::swap for every type that requires a specialized swap. Although this is legal C++, no Boost libraries use this method, whereas many Boost libraries provide specialized swap functions in their own namespaces. + The alternative to using argument dependent lookup in this situation is to provide a template specialization of std::swap for every type that requires a specialized swap. Although this is legal C++, no Boost libraries use this method, whereas many Boost libraries provide specialized swap functions in their own namespaces.

    - boost::swap also supports swapping built-in arrays. Note that std::swap doesn't yet do so, but a request to add an overload of std::swap for built-in arrays has been well-received by the Library Working Group of the C++ Standards Committee2. + boost::swap also supports swapping built-in arrays. Note that std::swap originally did not do so, but a request to add an overload of std::swap for built-in arrays has been accepted by the C++ Standards Committee2.

    @@ -45,11 +45,11 @@

    Or:

      -
    • A function with the signature swap(T&,T&) is available via argument dependent lookup
    • +
    • A function with the signature swap(T&,T&) is available via argument dependent lookup

    Or:

      -
    • A template specialization of std::swap exists for T
    • +
    • A template specialization of std::swap exists for T

    Or:

      @@ -73,18 +73,18 @@ Joseph Gauterin - for the initial idea, implementation, tests, and documentation
    • - Steven Watanabe - for the idea to make boost::swap less specialized than std::swap, thereby allowing the function to have the name 'swap' without introducing ambiguity + Steven Watanabe - for the idea to make boost::swap less specialized than std::swap, thereby allowing the function to have the name 'swap' without introducing ambiguity

    [1]Scott Meyers, Effective C++ Third Edition, Item 25: "Consider support for a non-throwing swap"

    -

    [2]LWG issue 809 (std::swap should be overloaded for array types)

    +

    [2]LWG Defect Report 809 (std::swap should be overloaded for array types)


    -

    Revised: 21 October 2008

    +

    Revised: 15 November 2008

    Copyright 2007, 2008 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.) From 8f99ebdc35a64ce5c4baa7bbe8990224b3e9c38e Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Fri, 21 Nov 2008 21:28:47 +0000 Subject: [PATCH 29/39] Added array_of_template test, testing the boost::swap utility on an array of objects of a template class. [SVN r49862] --- swap/test/Jamfile.v2 | 1 + swap/test/array_of_template.cpp | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 swap/test/array_of_template.cpp diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 index 7071837..617f7e8 100644 --- a/swap/test/Jamfile.v2 +++ b/swap/test/Jamfile.v2 @@ -29,5 +29,6 @@ test-suite utility/swap [ run std_vector_of_global.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run std_vector_of_other.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run no_ambiguity_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run array_of_template.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] ; diff --git a/swap/test/array_of_template.cpp b/swap/test/array_of_template.cpp new file mode 100644 index 0000000..e6e465f --- /dev/null +++ b/swap/test/array_of_template.cpp @@ -0,0 +1,68 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + +#include //for std::copy and std::equal +#include //for std::size_t + +template +class swap_test_template +{ +public: + swap_test_class swap_test_object; +}; + +template +inline bool operator==(const swap_test_template & lhs, const swap_test_template & rhs) +{ + return lhs.swap_test_object == rhs.swap_test_object; +} + +template +inline bool operator!=(const swap_test_template & lhs, const swap_test_template & rhs) +{ + return !(lhs == rhs); +} + +//Provide swap function in the namespace of swap_test_template +//(which is the global namespace). Note that it isn't allowed to put +//an overload of this function within the std namespace. +template +void swap(swap_test_template& left, swap_test_template& right) +{ + left.swap_test_object.swap(right.swap_test_object); +} + + +int test_main(int, char*[]) +{ + const std::size_t dimension = 2; + const swap_test_template initial_array1[dimension] = { swap_test_class(1), swap_test_class(2) }; + const swap_test_template initial_array2[dimension] = { swap_test_class(3), swap_test_class(4) }; + + swap_test_template array1[dimension]; + swap_test_template array2[dimension]; + + std::copy(initial_array1, initial_array1 + dimension, array1); + std::copy(initial_array2, initial_array2 + dimension, array2); + + swap_test_class::reset(); + boost::swap(array1, array2); + + BOOST_CHECK(std::equal(array1, array1 + dimension, initial_array2)); + BOOST_CHECK(std::equal(array2, array2 + dimension, initial_array1)); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); + + return 0; +} From 6a503ceaf5ce05a8e77485470a7fcd7339b4c6ef Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Mon, 24 Nov 2008 17:41:15 +0000 Subject: [PATCH 30/39] Replaced swap/test/swap_arrays by more specific tests: array_of_array, array_of_class, and array_of_int. [SVN r49916] --- swap/test/Jamfile.v2 | 6 +- .../{swap_arrays.cpp => array_of_array.cpp} | 35 +---------- swap/test/array_of_class.cpp | 59 +++++++++++++++++++ swap/test/array_of_int.cpp | 33 +++++++++++ 4 files changed, 97 insertions(+), 36 deletions(-) rename swap/test/{swap_arrays.cpp => array_of_array.cpp} (66%) create mode 100644 swap/test/array_of_class.cpp create mode 100644 swap/test/array_of_int.cpp diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 index 617f7e8..de1cd69 100644 --- a/swap/test/Jamfile.v2 +++ b/swap/test/Jamfile.v2 @@ -29,6 +29,8 @@ test-suite utility/swap [ run std_vector_of_global.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run std_vector_of_other.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run no_ambiguity_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run array_of_template.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run array_of_array.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run array_of_class.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run array_of_int.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run array_of_template.cpp ../../../test/build//boost_test_exec_monitor/static ] ; diff --git a/swap/test/swap_arrays.cpp b/swap/test/array_of_array.cpp similarity index 66% rename from swap/test/swap_arrays.cpp rename to swap/test/array_of_array.cpp index eeb64bc..06eee48 100644 --- a/swap/test/swap_arrays.cpp +++ b/swap/test/array_of_array.cpp @@ -33,32 +33,8 @@ namespace std } } -// Tests swapping 1-dimensional arrays. -void test_swapping_1D_arrays() -{ - const std::size_t dimension = 2; - const swap_test_class initial_array1[dimension] = { swap_test_class(1), swap_test_class(2) }; - const swap_test_class initial_array2[dimension] = { swap_test_class(3), swap_test_class(4) }; - - swap_test_class array1[dimension]; - swap_test_class array2[dimension]; - std::copy(initial_array1, initial_array1 + dimension, array1); - std::copy(initial_array2, initial_array2 + dimension, array2); - - swap_test_class::reset(); - boost::swap(array1, array2); - - BOOST_CHECK(std::equal(array1, array1 + dimension, initial_array2)); - BOOST_CHECK(std::equal(array2, array2 + dimension, initial_array1)); - - BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); - BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); -} - - -// Tests swapping 2-dimensional arrays. -void test_swapping_2D_arrays() +int test_main(int, char*[]) { const std::size_t first_dimension = 3; const std::size_t second_dimension = 4; @@ -76,7 +52,6 @@ void test_swapping_2D_arrays() ptr2[i].set_data( static_cast(i + number_of_elements) ); } - swap_test_class::reset(); boost::swap(array1, array2); for (std::size_t i = 0; i < number_of_elements; ++i) @@ -87,14 +62,6 @@ void test_swapping_2D_arrays() BOOST_CHECK_EQUAL(swap_test_class::swap_count(), number_of_elements); BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); -} - - -int test_main(int, char*[]) -{ - test_swapping_1D_arrays(); - test_swapping_2D_arrays(); return 0; } - diff --git a/swap/test/array_of_class.cpp b/swap/test/array_of_class.cpp new file mode 100644 index 0000000..df79801 --- /dev/null +++ b/swap/test/array_of_class.cpp @@ -0,0 +1,59 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + +#include //for std::copy and std::equal +#include //for std::size_t + +//Provide swap function in both the namespace of swap_test_class +//(which is the global namespace), and the std namespace. +//It's common to provide a swap function for a class in both +//namespaces. Scott Meyers recommends doing so: Effective C++, +//Third Edition, item 25, "Consider support for a non-throwing swap". +void swap(swap_test_class& left, swap_test_class& right) +{ + left.swap(right); +} + +namespace std +{ + template <> + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + + +int test_main(int, char*[]) +{ + const std::size_t array_size = 2; + const swap_test_class initial_array1[array_size] = { swap_test_class(1), swap_test_class(2) }; + const swap_test_class initial_array2[array_size] = { swap_test_class(3), swap_test_class(4) }; + + swap_test_class array1[array_size]; + swap_test_class array2[array_size]; + + std::copy(initial_array1, initial_array1 + array_size, array1); + std::copy(initial_array2, initial_array2 + array_size, array2); + + swap_test_class::reset(); + boost::swap(array1, array2); + + BOOST_CHECK(std::equal(array1, array1 + array_size, initial_array2)); + BOOST_CHECK(std::equal(array2, array2 + array_size, initial_array1)); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), array_size); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); + + return 0; +} diff --git a/swap/test/array_of_int.cpp b/swap/test/array_of_int.cpp new file mode 100644 index 0000000..f919613 --- /dev/null +++ b/swap/test/array_of_int.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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 +#define BOOST_INCLUDE_MAIN +#include + +#include //for std::copy and std::equal +#include //for std::size_t + + +int test_main(int, char*[]) +{ + const std::size_t array_size = 3; + const int initial_array1[array_size] = { 1, 2, 3 }; + const int initial_array2[array_size] = { 4, 5, 6 }; + + int array1[array_size]; + int array2[array_size]; + + std::copy(initial_array1, initial_array1 + array_size, array1); + std::copy(initial_array2, initial_array2 + array_size, array2); + + boost::swap(array1, array2); + + BOOST_CHECK(std::equal(array1, array1 + array_size, initial_array2)); + BOOST_CHECK(std::equal(array2, array2 + array_size, initial_array1)); + + return 0; +} From 30c7adb4c09097820875782da4d34a9a2c890b96 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Thu, 27 Nov 2008 11:08:05 +0000 Subject: [PATCH 31/39] Added comment to various array swapping tests, added member typedef to swap_test_template, to make the test more realistic. [SVN r49953] --- swap/test/array_of_class.cpp | 2 ++ swap/test/array_of_int.cpp | 2 ++ swap/test/array_of_template.cpp | 23 +++++++++++++---------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/swap/test/array_of_class.cpp b/swap/test/array_of_class.cpp index df79801..356762d 100644 --- a/swap/test/array_of_class.cpp +++ b/swap/test/array_of_class.cpp @@ -4,6 +4,8 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) +// Tests swapping an array of arrays of swap_test_class objects by means of boost::swap. + #include #define BOOST_INCLUDE_MAIN #include diff --git a/swap/test/array_of_int.cpp b/swap/test/array_of_int.cpp index f919613..22a7dde 100644 --- a/swap/test/array_of_int.cpp +++ b/swap/test/array_of_int.cpp @@ -4,6 +4,8 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) +// Tests swapping an array of integers by means of boost::swap. + #include #define BOOST_INCLUDE_MAIN #include diff --git a/swap/test/array_of_template.cpp b/swap/test/array_of_template.cpp index e6e465f..8f350ad 100644 --- a/swap/test/array_of_template.cpp +++ b/swap/test/array_of_template.cpp @@ -4,6 +4,8 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) +// Tests swapping an array of swap_test_template objects by means of boost::swap. + #include #define BOOST_INCLUDE_MAIN #include @@ -18,6 +20,7 @@ template class swap_test_template { public: + typedef T template_argument; swap_test_class swap_test_object; }; @@ -45,23 +48,23 @@ void swap(swap_test_template& left, swap_test_template& right) int test_main(int, char*[]) { - const std::size_t dimension = 2; - const swap_test_template initial_array1[dimension] = { swap_test_class(1), swap_test_class(2) }; - const swap_test_template initial_array2[dimension] = { swap_test_class(3), swap_test_class(4) }; + const std::size_t array_size = 2; + const swap_test_template initial_array1[array_size] = { swap_test_class(1), swap_test_class(2) }; + const swap_test_template initial_array2[array_size] = { swap_test_class(3), swap_test_class(4) }; - swap_test_template array1[dimension]; - swap_test_template array2[dimension]; + swap_test_template array1[array_size]; + swap_test_template array2[array_size]; - std::copy(initial_array1, initial_array1 + dimension, array1); - std::copy(initial_array2, initial_array2 + dimension, array2); + std::copy(initial_array1, initial_array1 + array_size, array1); + std::copy(initial_array2, initial_array2 + array_size, array2); swap_test_class::reset(); boost::swap(array1, array2); - BOOST_CHECK(std::equal(array1, array1 + dimension, initial_array2)); - BOOST_CHECK(std::equal(array2, array2 + dimension, initial_array1)); + BOOST_CHECK(std::equal(array1, array1 + array_size, initial_array2)); + BOOST_CHECK(std::equal(array2, array2 + array_size, initial_array1)); - BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), array_size); BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); return 0; From 20085f6d605174d1be4429ac79e1a21bdde65e5b Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Thu, 27 Nov 2008 11:14:52 +0000 Subject: [PATCH 32/39] Distinguished between testing array-of-array-of-class and array-of-array-of-int, as the latter appears to succeed on CodeGear 6.10 while the former does not. [SVN r49954] --- swap/test/Jamfile.v2 | 3 +- ..._array.cpp => array_of_array_of_class.cpp} | 2 + swap/test/array_of_array_of_int.cpp | 42 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) rename swap/test/{array_of_array.cpp => array_of_array_of_class.cpp} (95%) create mode 100644 swap/test/array_of_array_of_int.cpp diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 index de1cd69..f5b0d46 100644 --- a/swap/test/Jamfile.v2 +++ b/swap/test/Jamfile.v2 @@ -29,7 +29,8 @@ test-suite utility/swap [ run std_vector_of_global.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run std_vector_of_other.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run no_ambiguity_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] - [ run array_of_array.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run array_of_array_of_class.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run array_of_array_of_int.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run array_of_class.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run array_of_int.cpp ../../../test/build//boost_test_exec_monitor/static ] [ run array_of_template.cpp ../../../test/build//boost_test_exec_monitor/static ] diff --git a/swap/test/array_of_array.cpp b/swap/test/array_of_array_of_class.cpp similarity index 95% rename from swap/test/array_of_array.cpp rename to swap/test/array_of_array_of_class.cpp index 06eee48..4f02578 100644 --- a/swap/test/array_of_array.cpp +++ b/swap/test/array_of_array_of_class.cpp @@ -4,6 +4,8 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) +// Tests swapping an array of arrays of swap_test_class objects by means of boost::swap. + #include #define BOOST_INCLUDE_MAIN #include diff --git a/swap/test/array_of_array_of_int.cpp b/swap/test/array_of_array_of_int.cpp new file mode 100644 index 0000000..c1778ec --- /dev/null +++ b/swap/test/array_of_array_of_int.cpp @@ -0,0 +1,42 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// 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) + +// Tests swapping an array of arrays of integers by means of boost::swap. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include //for std::copy and std::equal +#include //for std::size_t + +int test_main(int, char*[]) +{ + const std::size_t first_dimension = 3; + const std::size_t second_dimension = 4; + const std::size_t number_of_elements = first_dimension * second_dimension; + + int array1[first_dimension][second_dimension]; + int array2[first_dimension][second_dimension]; + + int* const ptr1 = array1[0]; + int* const ptr2 = array2[0]; + + for (std::size_t i = 0; i < number_of_elements; ++i) + { + ptr1[i] = static_cast(i); + ptr2[i] = static_cast(i + number_of_elements); + } + + boost::swap(array1, array2); + + for (std::size_t i = 0; i < number_of_elements; ++i) + { + BOOST_CHECK_EQUAL(ptr1[i], static_cast(i + number_of_elements) ); + BOOST_CHECK_EQUAL(ptr2[i], static_cast(i) ); + } + return 0; +} From 6ab7c0401fa85387ce320fb6cddc74fd3b3a5d45 Mon Sep 17 00:00:00 2001 From: Jeremiah Willcock Date: Wed, 20 May 2009 19:19:00 +0000 Subject: [PATCH 33/39] Fixed most tab and min/max issues from trunk inspection report [SVN r53141] --- swap/test/std_bitset.cpp | 2 +- swap/test/swap_test_class.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/swap/test/std_bitset.cpp b/swap/test/std_bitset.cpp index 4b9fbae..d8d69e7 100644 --- a/swap/test/std_bitset.cpp +++ b/swap/test/std_bitset.cpp @@ -19,7 +19,7 @@ int test_main(int, char*[]) typedef std::bitset<8> bitset_type; const bitset_type initial_value1 = 1ul; const bitset_type initial_value2 = 2ul; - + bitset_type object1 = initial_value1; bitset_type object2 = initial_value2; diff --git a/swap/test/swap_test_class.hpp b/swap/test/swap_test_class.hpp index 8cf1fe9..a25dbbd 100644 --- a/swap/test/swap_test_class.hpp +++ b/swap/test/swap_test_class.hpp @@ -12,7 +12,7 @@ class swap_test_class { - int m_data; + int m_data; public: explicit swap_test_class(int arg = 0) : From 05d32deb4fbadb00cbe8518473060f05bf5e11bb Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Tue, 8 Sep 2009 16:54:54 +0000 Subject: [PATCH 34/39] Mentioned swap.hpp header, as requested by Thorsten Ottosen and David Abrahams [SVN r56107] --- swap.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/swap.html b/swap.html index 4022e0b..50722b2 100644 --- a/swap.html +++ b/swap.html @@ -7,7 +7,11 @@ +

    C++ Boost + Header <boost/swap.hpp> +

    +

    Swap

    From e26c813cd64a85f64f0d61ed6c6adc87f7529176 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Tue, 8 Sep 2009 17:07:13 +0000 Subject: [PATCH 35/39] Updated revision date of Boost Swap documentation [SVN r56108] --- swap.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swap.html b/swap.html index 50722b2..36f8425 100644 --- a/swap.html +++ b/swap.html @@ -88,9 +88,9 @@


    -

    Revised: 15 November 2008

    +

    Revised: 08 September 2009

    - Copyright 2007, 2008 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0. + Copyright 2007 - 2009 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)

    From 7dc1c4750e39954248fac16344d7fa84c019ceab Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 7 Mar 2010 21:42:22 +0000 Subject: [PATCH 36/39] Hopefully fixed #3984 (std::bitset constructor issue). Tested by Juergen Hunold on msvc-10.0, msvc-9.0, and gcc-4.4. See thread starting at http://lists.boost.org/Archives/boost/2010/03/162690.php [SVN r60331] --- swap/test/std_bitset.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swap/test/std_bitset.cpp b/swap/test/std_bitset.cpp index d8d69e7..8c0851a 100644 --- a/swap/test/std_bitset.cpp +++ b/swap/test/std_bitset.cpp @@ -17,8 +17,8 @@ int test_main(int, char*[]) { typedef std::bitset<8> bitset_type; - const bitset_type initial_value1 = 1ul; - const bitset_type initial_value2 = 2ul; + const bitset_type initial_value1 = 1; + const bitset_type initial_value2 = 2; bitset_type object1 = initial_value1; bitset_type object2 = initial_value2; From 93b1e7f52b93e7da28d0bb47d1bc32ce89bcfa6b Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Mon, 5 Apr 2010 19:08:01 +0000 Subject: [PATCH 37/39] Updated copyright notice. [SVN r61075] --- swap/test/std_bitset.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/test/std_bitset.cpp b/swap/test/std_bitset.cpp index 8c0851a..c46b686 100644 --- a/swap/test/std_bitset.cpp +++ b/swap/test/std_bitset.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// Copyright (c) 2008 - 2010 Joseph Gauterin, Niels Dekker // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at From 31b36d136de6f05b39247e927ba0d4bb02cd3f09 Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Thu, 1 May 2014 15:29:43 -0700 Subject: [PATCH 38/39] value_init and swap work on nvidia gpu's --- include/boost/utility/swap.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/boost/utility/swap.hpp b/include/boost/utility/swap.hpp index 6845e79..5ee6dc1 100644 --- a/include/boost/utility/swap.hpp +++ b/include/boost/utility/swap.hpp @@ -21,12 +21,15 @@ // avoid ambiguity when swapping objects of a Boost type that does // not have its own boost::swap overload. -#include //for std::swap +#include //for std::swap (C++11) +#include //for std::swap (C++98) #include //for std::size_t +#include namespace boost_swap_impl { template + BOOST_GPU_ENABLED void swap_impl(T& left, T& right) { using namespace std;//use std::swap if argument dependent lookup fails @@ -34,6 +37,7 @@ namespace boost_swap_impl } template + BOOST_GPU_ENABLED void swap_impl(T (& left)[N], T (& right)[N]) { for (std::size_t i = 0; i < N; ++i) @@ -46,6 +50,7 @@ namespace boost_swap_impl namespace boost { template + BOOST_GPU_ENABLED void swap(T1& left, T2& right) { ::boost_swap_impl::swap_impl(left, right); From d0c9dca64db9066f4b67cf1c793f0cf6fa8006fb Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Sun, 1 Jun 2014 22:03:45 +0400 Subject: [PATCH 39/39] Corrected BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT macro name in one branch. --- include/boost/utility/explicit_operator_bool.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/utility/explicit_operator_bool.hpp b/include/boost/utility/explicit_operator_bool.hpp index e16f34d..df16d9e 100644 --- a/include/boost/utility/explicit_operator_bool.hpp +++ b/include/boost/utility/explicit_operator_bool.hpp @@ -134,7 +134,7 @@ namespace detail { return !this->operator! ();\ } -#define BOOST_EXPLICIT_OPERATOR_BOOL()\ +#define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\ BOOST_FORCEINLINE operator bool () const BOOST_NOEXCEPT\ {\ return !this->operator! ();\