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 | |
21 | struct S { |
22 | int* calls; |
23 | friend constexpr void swap(S& a, S& b) { |
24 | *a.calls += 1; |
25 | *b.calls += 1; |
26 | } |
27 | }; |
28 | struct 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 | |
36 | static_assert(std::is_swappable_v<std::tuple<>>); |
37 | static_assert(std::is_swappable_v<std::tuple<S>>); |
38 | static_assert(std::is_swappable_v<std::tuple<CS>>); |
39 | static_assert(std::is_swappable_v<std::tuple<S&>>); |
40 | static_assert(std::is_swappable_v<std::tuple<CS, S>>); |
41 | static_assert(std::is_swappable_v<std::tuple<CS, S&>>); |
42 | static_assert(std::is_swappable_v<const std::tuple<>>); |
43 | static_assert(!std::is_swappable_v<const std::tuple<S>>); |
44 | static_assert(std::is_swappable_v<const std::tuple<CS>>); |
45 | static_assert(std::is_swappable_v<const std::tuple<S&>>); |
46 | static_assert(!std::is_swappable_v<const std::tuple<CS, S>>); |
47 | static_assert(std::is_swappable_v<const std::tuple<CS, S&>>); |
48 | |
49 | constexpr 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 | |
63 | int main(int, char**) { |
64 | test(); |
65 | static_assert(test()); |
66 | |
67 | return 0; |
68 | } |
69 | |