• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 Copyright 2018 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4 
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
7 */
8 #include <boost/config.hpp>
9 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
10 #include <boost/core/exchange.hpp>
11 #include <boost/core/lightweight_test.hpp>
12 
13 class C1 {
14 public:
C1(int i)15     explicit C1(int i)
16         : i_(i) { }
C1(C1 && c)17     C1(C1&& c)
18         : i_(c.i_) { }
operator =(C1 && c)19     C1& operator=(C1&& c) {
20         i_ = c.i_;
21         return *this;
22     }
i() const23     int i() const {
24         return i_;
25     }
26 private:
27     C1(const C1&);
28     C1& operator=(const C1&);
29     int i_;
30 };
31 
test1()32 void test1()
33 {
34     C1 x(1);
35     BOOST_TEST(boost::exchange(x, C1(2)).i() == 1);
36     BOOST_TEST(x.i() == 2);
37 }
38 
39 class C2 {
40 public:
C2(int i)41     explicit C2(int i)
42         : i_(i) { }
operator C1() const43     operator C1() const {
44         return C1(i_);
45     }
i() const46     int i() const {
47         return i_;
48     }
49 private:
50     C2(const C2&);
51     C2& operator=(const C2&);
52     int i_;
53 };
54 
test2()55 void test2()
56 {
57     C1 x(1);
58     BOOST_TEST(boost::exchange(x, C2(2)).i() == 1);
59     BOOST_TEST(x.i() == 2);
60 }
61 
62 class C3 {
63 public:
C3(int i)64     explicit C3(int i)
65         : i_(i) { }
C3(C3 && c)66     C3(C3&& c)
67         : i_(c.i_) { }
operator =(C1 && c)68     C3& operator=(C1&& c) {
69         i_ = c.i();
70         return *this;
71     }
i() const72     int i() const {
73         return i_;
74     }
75 private:
76     C3(const C3&);
77     C3& operator=(const C3&);
78     int i_;
79 };
80 
test3()81 void test3()
82 {
83     C3 x(1);
84     BOOST_TEST(boost::exchange(x, C1(2)).i() == 1);
85     BOOST_TEST(x.i() == 2);
86 }
87 
main()88 int main()
89 {
90     test1();
91     test2();
92     test3();
93     return boost::report_errors();
94 }
95 #else
main()96 int main()
97 {
98     return 0;
99 }
100 #endif
101