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// constexpr const pair& operator=(const pair& p) const;
15
16#include <cassert>
17#include <utility>
18
19#include "test_macros.h"
20#include "copy_move_types.h"
21
22// Constraints:
23// is_copy_assignable<const first_type> is true and
24// is_copy_assignable<const second_type> is true.
25
26// clang-format off
27static_assert(std::is_assignable_v<const std::pair<int&, int&>&,
28 const std::pair<int&, int&>&>);
29static_assert(!std::is_assignable_v<const std::pair<int, int>&,
30 const std::pair<int, int>&>);
31static_assert(!std::is_assignable_v<const std::pair<int, int&>&,
32 const std::pair<int, int&>&>);
33static_assert(!std::is_assignable_v<const std::pair<int&, int>&,
34 const std::pair<int&, int>&>);
35
36static_assert(std::is_assignable_v<const std::pair<ConstCopyAssign, ConstCopyAssign>&,
37 const std::pair<ConstCopyAssign, ConstCopyAssign>&>);
38static_assert(!std::is_assignable_v<const std::pair<CopyAssign, CopyAssign>&,
39 const std::pair<CopyAssign, CopyAssign>&>);
40
41// clang-format on
42
43constexpr bool test() {
44 // reference types
45 {
46 int i1 = 1;
47 int i2 = 2;
48 double d1 = 3.0;
49 double d2 = 5.0;
50 const std::pair<int&, double&> p1{i1, d1};
51 const std::pair<int&, double&> p2{i2, d2};
52 p2 = p1;
53 assert(p2.first == 1);
54 assert(p2.second == 3.0);
55 }
56
57 // user defined const copy assignment
58 {
59 const std::pair<ConstCopyAssign, ConstCopyAssign> p1{1, 2};
60 const std::pair<ConstCopyAssign, ConstCopyAssign> p2{3, 4};
61 p2 = p1;
62 assert(p2.first.val == 1);
63 assert(p2.second.val == 2);
64 }
65
66 // The correct assignment operator of the underlying type is used
67 {
68 std::pair<TracedAssignment, const TracedAssignment> t1{};
69 const std::pair<TracedAssignment, const TracedAssignment> t2{};
70 t2 = t1;
71 assert(t2.first.constCopyAssign == 1);
72 assert(t2.second.constCopyAssign == 1);
73 }
74
75 return true;
76}
77
78int main(int, char**) {
79 test();
80// gcc cannot have mutable member in constant expression
81#if !defined(TEST_COMPILER_GCC)
82 static_assert(test());
83#endif
84 return 0;
85}
86

source code of libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_const_copy_pair.pass.cpp