| 1 | // Copyright Louis Dionne 2013-2022 |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 4 | |
| 5 | #include <boost/hana/assert.hpp> |
| 6 | #include <boost/hana/config.hpp> |
| 7 | #include <boost/hana/first.hpp> |
| 8 | #include <boost/hana/pair.hpp> |
| 9 | #include <boost/hana/second.hpp> |
| 10 | |
| 11 | #include <utility> |
| 12 | namespace hana = boost::hana; |
| 13 | |
| 14 | |
| 15 | struct MoveOnly { |
| 16 | int data_; |
| 17 | MoveOnly(MoveOnly const&) = delete; |
| 18 | MoveOnly& operator=(MoveOnly const&) = delete; |
| 19 | MoveOnly(int data = 1) : data_(data) { } |
| 20 | MoveOnly(MoveOnly&& x) : data_(x.data_) { x.data_ = 0; } |
| 21 | |
| 22 | MoveOnly& operator=(MoveOnly&& x) |
| 23 | { data_ = x.data_; x.data_ = 0; return *this; } |
| 24 | |
| 25 | bool operator==(const MoveOnly& x) const { return data_ == x.data_; } |
| 26 | }; |
| 27 | |
| 28 | struct MoveOnlyDerived : MoveOnly { |
| 29 | MoveOnlyDerived(MoveOnlyDerived&&) = default; |
| 30 | MoveOnlyDerived(int data = 1) : MoveOnly(data) { } |
| 31 | }; |
| 32 | |
| 33 | constexpr auto in_constexpr_context(int a, short b) { |
| 34 | hana::pair<int, short> p1(a, b); |
| 35 | hana::pair<int, short> p2; |
| 36 | hana::pair<double, long> p3; |
| 37 | p2 = std::move(p1); |
| 38 | p3 = std::move(p2); |
| 39 | return p3; |
| 40 | } |
| 41 | |
| 42 | int main() { |
| 43 | // from pair<T, U> to pair<T, U> |
| 44 | { |
| 45 | hana::pair<MoveOnly, short> p1(MoveOnly{3}, 4); |
| 46 | hana::pair<MoveOnly, short> p2; |
| 47 | p2 = std::move(p1); |
| 48 | BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == MoveOnly{3}); |
| 49 | BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 4); |
| 50 | } |
| 51 | |
| 52 | // from pair<T, U> to pair<V, W> |
| 53 | { |
| 54 | hana::pair<MoveOnlyDerived, short> p1(MoveOnlyDerived{3}, 4); |
| 55 | hana::pair<MoveOnly, long> p2; |
| 56 | p2 = std::move(p1); |
| 57 | BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == MoveOnly{3}); |
| 58 | BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 4); |
| 59 | } |
| 60 | |
| 61 | // make sure that also works in a constexpr context |
| 62 | // (test fails under GCC <= 6 due to buggy constexpr) |
| 63 | #if BOOST_HANA_CONFIG_GCC >= BOOST_HANA_CONFIG_VERSION(7, 0, 0) |
| 64 | { |
| 65 | constexpr auto p = in_constexpr_context(3, 4); |
| 66 | static_assert(hana::first(p) == 3, "" ); |
| 67 | static_assert(hana::second(p) == 4, "" ); |
| 68 | } |
| 69 | #endif |
| 70 | } |
| 71 | |