3 //2 conversions: rv<T> & and const rv<T> &
4 //1 rv<T> & constructor: move constructor
5 //1 const rv<T> & constructor: copy constructor
6 //1 T & constructor: copy constructor
9 //Since RVO is better than move-construction,
10 //avoid copy constructor overloading.
11 #include <boost/move/detail/config_begin.hpp>
12 #include <boost/move/utility_core.hpp>
19 BOOST_COPYABLE_AND_MOVABLE(obj)
24 std::cout << "constructing obj" << "\n";
32 std::cout << "copy construct from const obj" << "\n";
35 // copy construct from movable object (non-const rvalue, explicitly moved lvalue)
36 obj(BOOST_RV_REF(obj))
38 std::cout << "move construct from movable rvalue" << "\n";
41 obj& operator =(BOOST_COPY_ASSIGN_REF(obj))
43 std::cout << "copy assign from const obj" << "\n";
47 obj& operator =(BOOST_RV_REF(obj))
49 std::cout << "move assign from movable rvalue" << "\n";
55 obj rvalue_func() { return obj(); }
56 const obj const_rvalue_func() { return obj(); }
57 obj& lvalue_func() { static obj o; return o; }
58 const obj& const_lvalue_func() { static obj o; return o; }
60 obj produce() { return obj(); }
66 { consume(produce()); }
67 { obj o = produce(); }
70 obj o1(rvalue_func());
71 obj o2 = const_rvalue_func();
72 obj o3 = lvalue_func();
73 obj o4 = const_lvalue_func();
74 // can't explicitly move temporaries
75 //obj o5 = boost::move(rvalue_func());
77 //Maybe missed optimization: copied
79 //Explicit forward works OK and optimized
80 o5 = boost::forward<obj>(rvalue_func());
82 obj o7 = boost::move(lvalue_func());
83 obj o8 = boost::move(const_lvalue_func());
87 o = const_rvalue_func();
89 o = const_lvalue_func();
90 // can't explicitly move temporaries
91 //o = boost::move(rvalue_func());
92 o = boost::forward<obj>(rvalue_func());
93 o = boost::move(const_rvalue_func());
94 o = boost::move(lvalue_func());
95 o = boost::move(const_lvalue_func());
100 //We need to declare:
102 //2 conversions: rv<T> & and const rv<T> &
103 //1 rv<T> & constructor: move constructor
104 //1 const rv<T> & constructor: copy constructor
105 //1 T & constructor: copy constructor
107 #include <boost/move/detail/config_end.hpp>