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, c++11, c++14, c++17, c++20 |
10 | |
11 | // <utility> |
12 | |
13 | // template <class T1, class T2> struct pair |
14 | |
15 | // template <class U1, class U2> |
16 | // constexpr explicit(see below) pair(const pair<U1, U2>&& p); |
17 | |
18 | #include <cassert> |
19 | #include <utility> |
20 | |
21 | #include "copy_move_types.h" |
22 | #include "test_macros.h" |
23 | |
24 | // Constraints: |
25 | // is_constructible_v<T1, decltype(get<0>(FWD(p)))> is true and |
26 | // is_constructible_v<T2, decltype(get<1>(FWD(p)))> is true. |
27 | struct X {}; |
28 | struct Y {}; |
29 | struct NotConvertibleToXorY {}; |
30 | |
31 | static_assert(std::is_constructible_v<std::pair<X, Y>, const std::pair<X, Y>&&>); |
32 | static_assert(!std::is_constructible_v<std::pair<X, Y>, const std::pair<NotConvertibleToXorY, Y>&&>); |
33 | static_assert(!std::is_constructible_v<std::pair<X, Y>, const std::pair<X, NotConvertibleToXorY>&&>); |
34 | static_assert(!std::is_constructible_v<std::pair<X, Y>, const std::pair<NotConvertibleToXorY, NotConvertibleToXorY>&&>); |
35 | |
36 | // The expression inside explicit is equivalent to: |
37 | // !is_convertible_v<decltype(get<0>(FWD(p))), first_type> || |
38 | // !is_convertible_v<decltype(get<1>(FWD(p))), second_type>. |
39 | // clang-format off |
40 | static_assert( std::is_convertible_v<const std::pair<X, Y>&&, |
41 | std::pair<ConvertibleFrom<X>, ConvertibleFrom<Y>>>); |
42 | static_assert(!std::is_convertible_v<const std::pair<X, Y>&&, |
43 | std::pair<ConvertibleFrom<X>, ExplicitConstructibleFrom<Y>>>); |
44 | static_assert(!std::is_convertible_v<const std::pair<X, Y>&&, |
45 | std::pair<ExplicitConstructibleFrom<X>, ConvertibleFrom<Y>>>); |
46 | static_assert(!std::is_convertible_v<const std::pair<X, Y>&&, |
47 | std::pair<ExplicitConstructibleFrom<X>, ExplicitConstructibleFrom<Y>>>); |
48 | // clang-format on |
49 | |
50 | constexpr bool test() { |
51 | // simple case: init pair<const T&&, const U&&> from const pair<T, U>&& |
52 | { |
53 | const std::pair<int, int> p1{1, 2}; |
54 | std::pair<const int&&, const int&&> p2{std::move(p1)}; |
55 | assert(&(p2.first) == &(p1.first)); |
56 | assert(&(p2.second) == &(p1.second)); |
57 | } |
58 | |
59 | // test implicit conversions. |
60 | { |
61 | const std::pair<ConstMove, int> p1{1, 2}; |
62 | std::pair<ConvertibleFrom<ConstMove>, ConvertibleFrom<int>> p2 = std::move(p1); |
63 | assert(p2.first.v.val == 1); |
64 | assert(p2.second.v == 2); |
65 | } |
66 | |
67 | // test explicit conversions. |
68 | { |
69 | const std::pair<ConstMove, int> p1{1, 2}; |
70 | std::pair<ExplicitConstructibleFrom<ConstMove>, ExplicitConstructibleFrom<int>> p2{std::move(p1)}; |
71 | assert(p2.first.v.val == 1); |
72 | assert(p2.second.v == 2); |
73 | } |
74 | |
75 | // test correct constructors of underlying types are called |
76 | { |
77 | const std::pair<TracedCopyMove, TracedCopyMove> p1{}; |
78 | std::pair<ConvertibleFrom<TracedCopyMove>, ConvertibleFrom<TracedCopyMove>> p2{std::move(p1)}; |
79 | assert(constMoveCtrCalled(p2.first.v)); |
80 | assert(constMoveCtrCalled(p2.second.v)); |
81 | } |
82 | |
83 | return true; |
84 | } |
85 | |
86 | int main(int, char**) { |
87 | test(); |
88 | static_assert(test()); |
89 | return 0; |
90 | } |
91 | |