xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.special/non_member_swap_const.pass.cpp (revision 16719cd011a4d1a856ddc24dd80703172bfcfffe)
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;
swap(S & a,S & b)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;
swap(const CS & a,const CS & b)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 
test()49 constexpr bool test() {
50   int cs_calls = 0;
51   int s_calls = 0;
52   S s1{&s_calls};
53   S s2{&s_calls};
54   const std::tuple<CS, S&> t1 = {CS{&cs_calls}, s1};
55   const std::tuple<CS, S&> t2 = {CS{&cs_calls}, s2};
56   swap(t1, t2);
57   assert(cs_calls == 2);
58   assert(s_calls == 2);
59 
60   return true;
61 }
62 
main(int,char **)63 int main(int, char**) {
64   test();
65   static_assert(test());
66 
67   return 0;
68 }
69