xref: /llvm-project/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.move.pass.cpp (revision da876a157d7985f7f8de906180fa5ad7b685724a)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <utility>
12 
13 // template <class T1, class T2> struct pair
14 
15 // pair(pair&&) = default;
16 
17 #include <cassert>
18 #include <memory>
19 #include <type_traits>
20 #include <utility>
21 
22 #include "test_macros.h"
23 
24 struct Dummy {
25   Dummy(Dummy const&) = delete;
26   Dummy(Dummy &&) = default;
27 };
28 
29 struct NotCopyOrMoveConstructible {
30   NotCopyOrMoveConstructible() = default;
31   NotCopyOrMoveConstructible(NotCopyOrMoveConstructible const&) = delete;
32   NotCopyOrMoveConstructible(NotCopyOrMoveConstructible&&) = delete;
33 };
34 
main(int,char **)35 int main(int, char**)
36 {
37     {
38         typedef std::pair<int, short> P1;
39         static_assert(std::is_move_constructible<P1>::value, "");
40         P1 p1(3, static_cast<short>(4));
41         P1 p2 = std::move(p1);
42         assert(p2.first == 3);
43         assert(p2.second == 4);
44     }
45     {
46         using P = std::pair<Dummy, int>;
47         static_assert(!std::is_copy_constructible<P>::value, "");
48         static_assert(std::is_move_constructible<P>::value, "");
49     }
50     {
51         // When constructing a pair containing a reference, we only bind the
52         // reference, so it doesn't matter whether the type is or isn't
53         // copy/move constructible.
54         {
55             using P = std::pair<NotCopyOrMoveConstructible&, int>;
56             static_assert(std::is_move_constructible<P>::value, "");
57 
58             NotCopyOrMoveConstructible obj;
59             P p2{obj, 3};
60             P p1(std::move(p2));
61             assert(&p1.first == &obj);
62             assert(&p2.first == &obj);
63         }
64         {
65             using P = std::pair<NotCopyOrMoveConstructible&&, int>;
66             static_assert(std::is_move_constructible<P>::value, "");
67 
68             NotCopyOrMoveConstructible obj;
69             P p2{std::move(obj), 3};
70             P p1(std::move(p2));
71             assert(&p1.first == &obj);
72             assert(&p2.first == &obj);
73         }
74     }
75 
76     return 0;
77 }
78