Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / utility / pairs / pairs.spec / non_member_const_swap.pass.cpp
blob8610cfb3b544c455546afb675d027f0903b4b0e9
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 //===----------------------------------------------------------------------===//
9 // <utility>
11 // template <class T1, class T2>
12 // void swap(const pair<T1, T2>& x, const pair<T1, T2>& y) const noexcept(noexcept(x.swap(y)));;
14 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
16 #include <cassert>
17 #include <type_traits>
18 #include <utility>
20 #include "test_macros.h"
22 // Constraints:
23 // For the second overload, is_swappable_v<const T1> is true and is_swappable_v<const T2> is true.
24 struct NonConstSwappable {
25 friend constexpr void swap(NonConstSwappable&, NonConstSwappable&);
28 struct ConstSwappable {
29 mutable int i;
30 friend constexpr void swap(const ConstSwappable& lhs, const ConstSwappable& rhs) { std::swap(lhs.i, rhs.i); }
33 static_assert(std::is_swappable_v<const std::pair<ConstSwappable, ConstSwappable>>);
34 static_assert(!std::is_swappable_v<const std::pair<NonConstSwappable, ConstSwappable>>);
35 static_assert(!std::is_swappable_v<const std::pair<ConstSwappable, NonConstSwappable>>);
36 static_assert(!std::is_swappable_v<const std::pair<NonConstSwappable, NonConstSwappable>>);
38 // noexcept(noexcept(x.swap(y)));
39 template <class T>
40 concept NonMemberSwapNoexcept =
41 requires(T t1, T t2) {
42 { swap(t1, t2) } noexcept;
45 template <bool canThrow>
46 struct SwapMayThrow {};
48 template <bool canThrow>
49 void swap(const SwapMayThrow<canThrow>&, const SwapMayThrow<canThrow>&) noexcept(!canThrow);
51 static_assert(NonMemberSwapNoexcept<const std::pair<SwapMayThrow<false>, SwapMayThrow<false>>>);
52 static_assert(!NonMemberSwapNoexcept<const std::pair<SwapMayThrow<true>, SwapMayThrow<false>>>);
53 static_assert(!NonMemberSwapNoexcept<const std::pair<SwapMayThrow<false>, SwapMayThrow<true>>>);
54 static_assert(!NonMemberSwapNoexcept<const std::pair<SwapMayThrow<true>, SwapMayThrow<true>>>);
56 constexpr bool test() {
57 // user defined const swap
59 using P = std::pair<const ConstSwappable, const ConstSwappable>;
60 const P p1(ConstSwappable{0}, ConstSwappable{1});
61 const P p2(ConstSwappable{2}, ConstSwappable{3});
62 using std::swap;
63 swap(p1, p2);
64 assert(p1.first.i == 2);
65 assert(p1.second.i == 3);
66 assert(p2.first.i == 0);
67 assert(p2.second.i == 1);
70 // pair of references
72 int i1 = 0, i2 = 1, i3 = 2, i4 = 3;
73 const std::pair<int&, int&> p1{i1, i2};
74 const std::pair<int&, int&> p2{i3, i4};
75 using std::swap;
76 swap(p1, p2);
77 assert(p1.first == 2);
78 assert(p1.second == 3);
79 assert(p2.first == 0);
80 assert(p2.second == 1);
82 return true;
85 int main(int, char**) {
86 test();
88 // gcc cannot have mutable member in constant expression
89 #if !defined(TEST_COMPILER_GCC)
90 static_assert(test());
91 #endif
93 return 0;