xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/const_pair.pass.cpp (revision 06e2b737aa0347b42e8bf37cb00a053eab0a9393)
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 // <tuple>
10 
11 // template <class... Types> class tuple;
12 
13 // template <class U1, class U2>
14 //   tuple& operator=(const pair<U1, U2>& u);
15 
16 // UNSUPPORTED: c++03
17 
18 #include <cassert>
19 #include <memory>
20 #include <tuple>
21 #include <type_traits>
22 #include <utility>
23 
24 struct NothrowCopyAssignable {
25     NothrowCopyAssignable& operator=(NothrowCopyAssignable const&) noexcept { return *this; }
26 };
27 struct PotentiallyThrowingCopyAssignable {
28     PotentiallyThrowingCopyAssignable& operator=(PotentiallyThrowingCopyAssignable const&) { return *this; }
29 };
30 
31 #include "test_macros.h"
32 
33 TEST_CONSTEXPR_CXX20
34 bool test()
35 {
36     {
37         typedef std::pair<long, char> T0;
38         typedef std::tuple<long long, short> T1;
39         T0 t0(2, 'a');
40         T1 t1;
41         t1 = t0;
42         assert(std::get<0>(t1) == 2);
43         assert(std::get<1>(t1) == short('a'));
44     }
45     return true;
46 }
47 
48 int main(int, char**)
49 {
50     test();
51 #if TEST_STD_VER >= 20
52     static_assert(test());
53 #endif
54 
55     {
56         // test that the implicitly generated copy assignment operator
57         // is properly deleted
58         using T = std::tuple<int, int>;
59         using P = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
60         static_assert(!std::is_assignable<T&, const P &>::value, "");
61     }
62     {
63         typedef std::tuple<NothrowCopyAssignable, long> Tuple;
64         typedef std::pair<NothrowCopyAssignable, int> Pair;
65         static_assert(std::is_nothrow_assignable<Tuple&, Pair const&>::value, "");
66         static_assert(std::is_nothrow_assignable<Tuple&, Pair&>::value, "");
67         static_assert(std::is_nothrow_assignable<Tuple&, Pair const&&>::value, "");
68     }
69     {
70         typedef std::tuple<PotentiallyThrowingCopyAssignable, long> Tuple;
71         typedef std::pair<PotentiallyThrowingCopyAssignable, int> Pair;
72         static_assert(std::is_assignable<Tuple&, Pair const&>::value, "");
73         static_assert(!std::is_nothrow_assignable<Tuple&, Pair const&>::value, "");
74 
75         static_assert(std::is_assignable<Tuple&, Pair&>::value, "");
76         static_assert(!std::is_nothrow_assignable<Tuple&, Pair&>::value, "");
77 
78         static_assert(std::is_assignable<Tuple&, Pair const&&>::value, "");
79         static_assert(!std::is_nothrow_assignable<Tuple&, Pair const&&>::value, "");
80     }
81 
82     return 0;
83 }
84