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