| 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/first.hpp> |
| 7 | #include <boost/hana/pair.hpp> |
| 8 | #include <boost/hana/second.hpp> |
| 9 | namespace hana = boost::hana; |
| 10 | |
| 11 | |
| 12 | struct MoveOnly { |
| 13 | int data_; |
| 14 | MoveOnly(MoveOnly const&) = delete; |
| 15 | MoveOnly& operator=(MoveOnly const&) = delete; |
| 16 | MoveOnly(int data) : data_(data) { } |
| 17 | MoveOnly(MoveOnly&& x) : data_(x.data_) { x.data_ = 0; } |
| 18 | |
| 19 | MoveOnly& operator=(MoveOnly&& x) |
| 20 | { data_ = x.data_; x.data_ = 0; return *this; } |
| 21 | |
| 22 | bool operator==(const MoveOnly& x) const { return data_ == x.data_; } |
| 23 | }; |
| 24 | |
| 25 | class FromInt { |
| 26 | int data_; |
| 27 | public: |
| 28 | constexpr FromInt(int data) : data_(data) { } |
| 29 | constexpr bool operator==(const FromInt& x) const { return data_ == x.data_; } |
| 30 | }; |
| 31 | |
| 32 | int main() { |
| 33 | ////////////////////////////////////////////////////////////////////////// |
| 34 | // Check for the pair(T&&, U&&) constructor |
| 35 | ////////////////////////////////////////////////////////////////////////// |
| 36 | { |
| 37 | hana::pair<MoveOnly, short*> p(MoveOnly{3}, nullptr); |
| 38 | BOOST_HANA_RUNTIME_CHECK(hana::first(p) == MoveOnly{3}); |
| 39 | BOOST_HANA_RUNTIME_CHECK(hana::second(p) == nullptr); |
| 40 | } |
| 41 | |
| 42 | ////////////////////////////////////////////////////////////////////////// |
| 43 | // Check for the pair(First const&, Second const&) constructor |
| 44 | ////////////////////////////////////////////////////////////////////////// |
| 45 | { |
| 46 | hana::pair<float, short*> p1(3.5f, 0); |
| 47 | BOOST_HANA_RUNTIME_CHECK(hana::first(p1) == 3.5f); |
| 48 | BOOST_HANA_RUNTIME_CHECK(hana::second(p1) == nullptr); |
| 49 | |
| 50 | // brace init |
| 51 | hana::pair<float, short*> p2 = {3.5f, 0}; |
| 52 | BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == 3.5f); |
| 53 | BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == nullptr); |
| 54 | } |
| 55 | { |
| 56 | hana::pair<FromInt, int> p1(1, 2); |
| 57 | BOOST_HANA_RUNTIME_CHECK(hana::first(p1) == FromInt(1)); |
| 58 | BOOST_HANA_RUNTIME_CHECK(hana::second(p1) == 2); |
| 59 | |
| 60 | // brace init |
| 61 | hana::pair<FromInt, int> p2 = {1, 2}; |
| 62 | BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == FromInt(1)); |
| 63 | BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 2); |
| 64 | } |
| 65 | |
| 66 | // Make sure the above works constexpr too |
| 67 | { |
| 68 | constexpr hana::pair<float, short*> p1(3.5f, 0); |
| 69 | static_assert(hana::first(p1) == 3.5f, "" ); |
| 70 | static_assert(hana::second(p1) == nullptr, "" ); |
| 71 | |
| 72 | // brace init |
| 73 | constexpr hana::pair<float, short*> p2 = {3.5f, 0}; |
| 74 | static_assert(hana::first(p2) == 3.5f, "" ); |
| 75 | static_assert(hana::second(p2) == nullptr, "" ); |
| 76 | } |
| 77 | { |
| 78 | constexpr hana::pair<FromInt, int> p1(1, 2); |
| 79 | static_assert(hana::first(p1) == FromInt(1), "" ); |
| 80 | static_assert(hana::second(p1) == 2, "" ); |
| 81 | |
| 82 | // brace init |
| 83 | constexpr hana::pair<FromInt, int> p2 = {1, 2}; |
| 84 | static_assert(hana::first(p2) == FromInt(1), "" ); |
| 85 | static_assert(hana::second(p2) == 2, "" ); |
| 86 | } |
| 87 | } |
| 88 | |