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/meta_utils_core.hpp> 13 14 #include <boost/move/move.hpp> 15 16 //[template_assign_example_foo_bar 17 18 class Foo 19 { 20 BOOST_COPYABLE_AND_MOVABLE(Foo) 21 22 public: 23 int i; Foo(int val)24 explicit Foo(int val) : i(val) {} 25 Foo(BOOST_RV_REF (Foo)obj)26 Foo(BOOST_RV_REF(Foo) obj) : i(obj.i) {} 27 operator =(BOOST_RV_REF (Foo)rhs)28 Foo& operator=(BOOST_RV_REF(Foo) rhs) 29 { i = rhs.i; rhs.i = 0; return *this; } 30 operator =(BOOST_COPY_ASSIGN_REF (Foo)rhs)31 Foo& operator=(BOOST_COPY_ASSIGN_REF(Foo) rhs) 32 { i = rhs.i; return *this; } //(1) 33 34 template<class U> //(*) TEMPLATED ASSIGNMENT, potential problem 35 //<- 36 #if 1 37 typename ::boost::move_detail::disable_if_same<U, Foo, Foo&>::type operator =(const U & rhs)38 operator=(const U& rhs) 39 #else 40 //-> 41 Foo& operator=(const U& rhs) 42 //<- 43 #endif 44 //-> 45 { i = -rhs.i; return *this; } //(2) 46 }; 47 //] 48 49 struct Bar 50 { 51 int i; BarBar52 explicit Bar(int val) : i(val) {} 53 }; 54 55 56 //<- 57 #ifdef NDEBUG 58 #undef NDEBUG 59 #endif 60 //-> 61 #include <cassert> 62 main()63int main() 64 { 65 //[template_assign_example_main 66 Foo foo1(1); 67 //<- 68 assert(foo1.i == 1); 69 //-> 70 Foo foo2(2); 71 //<- 72 assert(foo2.i == 2); 73 Bar bar(3); 74 assert(bar.i == 3); 75 //-> 76 foo2 = foo1; // Calls (1) in C++11 but (2) in C++98 77 //<- 78 assert(foo2.i == 1); 79 assert(foo1.i == 1); //Fails in C++98 unless workaround is applied 80 foo1 = bar; 81 assert(foo1.i == -3); 82 foo2 = boost::move(foo1); 83 assert(foo1.i == 0); 84 assert(foo2.i == -3); 85 //-> 86 const Foo foo5(5); 87 foo2 = foo5; // Calls (1) in C++11 but (2) in C++98 88 //<- 89 assert(foo2.i == 5); //Fails in C++98 unless workaround is applied 90 assert(foo5.i == 5); 91 //-> 92 //] 93 return 0; 94 } 95 96