• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2014-2014.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // See http://www.boost.org/libs/move for documentation.
9 //
10 //////////////////////////////////////////////////////////////////////////////
11 
12 #include <boost/move/detail/config_begin.hpp>
13 #include <boost/move/detail/meta_utils_core.hpp>
14 
15 #include <boost/move/move.hpp>
16 
17 //[template_assign_example_foo_bar
18 
19 class Foo
20 {
21    BOOST_COPYABLE_AND_MOVABLE(Foo)
22 
23    public:
24    int i;
Foo(int val)25    explicit Foo(int val)      : i(val)   {}
26 
Foo(BOOST_RV_REF (Foo)obj)27    Foo(BOOST_RV_REF(Foo) obj) : i(obj.i) {}
28 
operator =(BOOST_RV_REF (Foo)rhs)29    Foo& operator=(BOOST_RV_REF(Foo) rhs)
30    {  i = rhs.i; rhs.i = 0; return *this; }
31 
operator =(BOOST_COPY_ASSIGN_REF (Foo)rhs)32    Foo& operator=(BOOST_COPY_ASSIGN_REF(Foo) rhs)
33    {  i = rhs.i; return *this;   } //(1)
34 
35    template<class U> //(*) TEMPLATED ASSIGNMENT, potential problem
36    //<-
37    #if 1
38    typename ::boost::move_detail::disable_if_same<U, Foo, Foo&>::type
operator =(const U & rhs)39    operator=(const U& rhs)
40    #else
41    //->
42    Foo& operator=(const U& rhs)
43    //<-
44    #endif
45    //->
46    {  i = -rhs.i; return *this;  } //(2)
47 };
48 //]
49 
50 struct Bar
51 {
52    int i;
BarBar53    explicit Bar(int val) : i(val) {}
54 };
55 
56 
57 //<-
58 #ifdef NDEBUG
59 #undef NDEBUG
60 #endif
61 //->
62 #include <cassert>
63 
main()64 int main()
65 {
66 //[template_assign_example_main
67    Foo foo1(1);
68    //<-
69    assert(foo1.i == 1);
70    //->
71    Foo foo2(2);
72    //<-
73    assert(foo2.i == 2);
74    Bar bar(3);
75    assert(bar.i == 3);
76    //->
77    foo2 = foo1; // Calls (1) in C++11 but (2) in C++98
78    //<-
79    assert(foo2.i == 1);
80    assert(foo1.i == 1); //Fails in C++98 unless workaround is applied
81    foo1 = bar;
82    assert(foo1.i == -3);
83    foo2 = boost::move(foo1);
84    assert(foo1.i == 0);
85    assert(foo2.i == -3);
86    //->
87    const Foo foo5(5);
88    foo2 = foo5; // Calls (1) in C++11 but (2) in C++98
89    //<-
90    assert(foo2.i == 5); //Fails in C++98 unless workaround is applied
91    assert(foo5.i == 5);
92    //->
93 //]
94    return 0;
95 }
96 
97 
98 #include <boost/move/detail/config_end.hpp>
99