1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2009.
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 //[construct_forward_example
13 #include <boost/move/utility_core.hpp>
14 #include <iostream>
15
16 class copyable_only_tester
17 {
18 public:
copyable_only_tester()19 copyable_only_tester()
20 { std::cout << "copyable_only_tester()" << std::endl; }
21
copyable_only_tester(const copyable_only_tester &)22 copyable_only_tester(const copyable_only_tester&)
23 { std::cout << "copyable_only_tester(const copyable_only_tester&)" << std::endl; }
24
copyable_only_tester(int)25 copyable_only_tester(int)
26 { std::cout << "copyable_only_tester(int)" << std::endl; }
27
copyable_only_tester(int,double)28 copyable_only_tester(int, double)
29 { std::cout << "copyable_only_tester(int, double)" << std::endl; }
30 };
31
32 class copyable_movable_tester
33 {
34 // move semantics
35 BOOST_COPYABLE_AND_MOVABLE(copyable_movable_tester)
36 public:
37
copyable_movable_tester()38 copyable_movable_tester()
39 { std::cout << "copyable_movable_tester()" << std::endl; }
40
copyable_movable_tester(int)41 copyable_movable_tester(int)
42 { std::cout << "copyable_movable_tester(int)" << std::endl; }
43
copyable_movable_tester(BOOST_RV_REF (copyable_movable_tester))44 copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester))
45 { std::cout << "copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester))" << std::endl; }
46
copyable_movable_tester(const copyable_movable_tester &)47 copyable_movable_tester(const copyable_movable_tester &)
48 { std::cout << "copyable_movable_tester(const copyable_movable_tester &)" << std::endl; }
49
copyable_movable_tester(BOOST_RV_REF (copyable_movable_tester),BOOST_RV_REF (copyable_movable_tester))50 copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester), BOOST_RV_REF(copyable_movable_tester))
51 { std::cout << "copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester), BOOST_RV_REF(copyable_movable_tester))" << std::endl; }
52
operator =(BOOST_RV_REF (copyable_movable_tester))53 copyable_movable_tester &operator=(BOOST_RV_REF(copyable_movable_tester))
54 { std::cout << "copyable_movable_tester & operator=(BOOST_RV_REF(copyable_movable_tester))" << std::endl;
55 return *this; }
56
operator =(BOOST_COPY_ASSIGN_REF (copyable_movable_tester))57 copyable_movable_tester &operator=(BOOST_COPY_ASSIGN_REF(copyable_movable_tester))
58 { std::cout << "copyable_movable_tester & operator=(BOOST_COPY_ASSIGN_REF(copyable_movable_tester))" << std::endl;
59 return *this; }
60 };
61
62 //1 argument
63 template<class MaybeMovable, class MaybeRv>
function_construct(BOOST_FWD_REF (MaybeRv)x)64 void function_construct(BOOST_FWD_REF(MaybeRv) x)
65 { MaybeMovable m(boost::forward<MaybeRv>(x)); }
66
67 //2 argument
68 template<class MaybeMovable, class MaybeRv, class MaybeRv2>
function_construct(BOOST_FWD_REF (MaybeRv)x,BOOST_FWD_REF (MaybeRv2)x2)69 void function_construct(BOOST_FWD_REF(MaybeRv) x, BOOST_FWD_REF(MaybeRv2) x2)
70 { MaybeMovable m(boost::forward<MaybeRv>(x), boost::forward<MaybeRv2>(x2)); }
71
main()72 int main()
73 {
74 copyable_movable_tester m;
75 //move constructor
76 function_construct<copyable_movable_tester>(boost::move(m));
77 //copy constructor
78 function_construct<copyable_movable_tester>(copyable_movable_tester());
79 //two rvalue constructor
80 function_construct<copyable_movable_tester>(boost::move(m), boost::move(m));
81
82 copyable_only_tester nm;
83 //copy constructor (copyable_only_tester has no move ctor.)
84 function_construct<copyable_only_tester>(boost::move(nm));
85 //copy constructor
86 function_construct<copyable_only_tester>(nm);
87 //int constructor
88 function_construct<copyable_only_tester>(int(0));
89 //int, double constructor
90 function_construct<copyable_only_tester>(int(0), double(0.0));
91
92 //Output is:
93 //copyable_movable_tester()
94 //copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester))
95 //copyable_movable_tester()
96 //copyable_movable_tester(const copyable_movable_tester &)
97 //copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester), BOOST_RV_REF(copyable_movable_tester))
98 //copyable_only_tester()
99 //copyable_only_tester(const copyable_only_tester&)
100 //copyable_only_tester(const copyable_only_tester&)
101 //copyable_only_tester(int)
102 //copyable_only_tester(int, double)
103 return 0;
104 }
105 //]
106