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... Types>
14// void swap(const tuple<Types...>& x, const tuple<Types...>& y);
15
16// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
17
18#include <tuple>
19#include <cassert>
20
21struct S {
22 int* calls;
23 friend constexpr void swap(S& a, S& b) {
24 *a.calls += 1;
25 *b.calls += 1;
26 }
27};
28struct CS {
29 int* calls;
30 friend constexpr void swap(const CS& a, const CS& b) {
31 *a.calls += 1;
32 *b.calls += 1;
33 }
34};
35
36static_assert(std::is_swappable_v<std::tuple<>>);
37static_assert(std::is_swappable_v<std::tuple<S>>);
38static_assert(std::is_swappable_v<std::tuple<CS>>);
39static_assert(std::is_swappable_v<std::tuple<S&>>);
40static_assert(std::is_swappable_v<std::tuple<CS, S>>);
41static_assert(std::is_swappable_v<std::tuple<CS, S&>>);
42static_assert(std::is_swappable_v<const std::tuple<>>);
43static_assert(!std::is_swappable_v<const std::tuple<S>>);
44static_assert(std::is_swappable_v<const std::tuple<CS>>);
45static_assert(std::is_swappable_v<const std::tuple<S&>>);
46static_assert(!std::is_swappable_v<const std::tuple<CS, S>>);
47static_assert(std::is_swappable_v<const std::tuple<CS, S&>>);
48
49constexpr bool test() {
50 int cs_calls = 0;
51 int s_calls = 0;
52 S s1{.calls: &s_calls};
53 S s2{.calls: &s_calls};
54 const std::tuple<CS, S&> t1 = {CS{.calls: &cs_calls}, s1};
55 const std::tuple<CS, S&> t2 = {CS{.calls: &cs_calls}, s2};
56 swap(t1, t2);
57 assert(cs_calls == 2);
58 assert(s_calls == 2);
59
60 return true;
61}
62
63int main(int, char**) {
64 test();
65 static_assert(test());
66
67 return 0;
68}
69

source code of libcxx/test/std/utilities/tuple/tuple.tuple/tuple.special/non_member_swap_const.pass.cpp