1// Boost.Function library
2
3// Copyright Douglas Gregor 2008. Use, modification and
4// distribution is subject to the Boost Software License, Version
5// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7
8// For more information, see http://www.boost.org
9
10#include <boost/function.hpp>
11#include <boost/core/lightweight_test.hpp>
12
13#define BOOST_CHECK BOOST_TEST
14
15struct tried_to_copy { };
16
17struct MaybeThrowOnCopy {
18 MaybeThrowOnCopy(int value = 0) : value(value) { }
19
20 MaybeThrowOnCopy(const MaybeThrowOnCopy& other) : value(other.value) {
21 if (throwOnCopy)
22 throw tried_to_copy();
23 }
24
25 MaybeThrowOnCopy& operator=(const MaybeThrowOnCopy& other) {
26 if (throwOnCopy)
27 throw tried_to_copy();
28 value = other.value;
29 return *this;
30 }
31
32 int operator()() { return value; }
33
34 int value;
35
36 // Make sure that this function object doesn't trigger the
37 // small-object optimization in Function.
38 float padding[100];
39
40 static bool throwOnCopy;
41};
42
43bool MaybeThrowOnCopy::throwOnCopy = false;
44
45int main()
46{
47 boost::function0<int> f;
48 boost::function0<int> g;
49
50 MaybeThrowOnCopy::throwOnCopy = false;
51 f = MaybeThrowOnCopy(1);
52 g = MaybeThrowOnCopy(2);
53 BOOST_CHECK(f() == 1);
54 BOOST_CHECK(g() == 2);
55
56 MaybeThrowOnCopy::throwOnCopy = true;
57 f.swap(other&: g);
58 BOOST_CHECK(f() == 2);
59 BOOST_CHECK(g() == 1);
60
61 return boost::report_errors();
62}
63

source code of boost/libs/function/test/nothrow_swap.cpp