]>
Commit | Line | Data |
---|---|---|
cfc219ae JW |
1 | // Copyright (C) 2019 Free Software Foundation, Inc. |
2 | // | |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // { dg-options "-std=gnu++2a" } | |
19 | // { dg-do compile { target c++2a } } | |
20 | ||
21 | #include <concepts> | |
22 | ||
23 | namespace nu | |
24 | { | |
25 | struct S { bool swapped = false; }; | |
26 | constexpr void swap(S& l, S& r) { l.swapped = r.swapped = true; } | |
27 | struct T { int i; }; | |
28 | ||
29 | union U { char c; int i; }; | |
30 | constexpr void swap(U& l, U& r) { l.i = r.i = 99; } | |
31 | } | |
32 | ||
33 | constexpr bool check_struct_with_adl_swap(int i) | |
34 | { | |
35 | nu::S s1, s2; | |
36 | std::ranges::swap(s1, s2); | |
37 | return s1.swapped && s2.swapped; | |
38 | } | |
39 | ||
40 | static_assert(check_struct_with_adl_swap(1)); | |
41 | ||
42 | constexpr bool check_array_with_adl_swap(int i) | |
43 | { | |
44 | nu::S s1[2], s2[2]; | |
45 | std::ranges::swap(s1, s2); | |
46 | return s1[0].swapped && s2[0].swapped && s1[1].swapped && s2[1].swapped; | |
47 | } | |
48 | ||
49 | static_assert(check_array_with_adl_swap(1)); | |
50 | ||
51 | constexpr bool check_struct_without_adl_swap(int i) | |
52 | { | |
53 | nu::T t1{i}, t2{2*i}; | |
54 | std::ranges::swap(t1, t2); | |
55 | return t1.i == 2*i && t2.i == i; | |
56 | } | |
57 | ||
58 | static_assert(check_struct_without_adl_swap(1)); | |
59 | ||
60 | constexpr bool check_array_without_adl_swap(int i) | |
61 | { | |
62 | nu::T t1[2]{i, 2*i}, t2[2]{3*i, 4*i}; | |
63 | std::ranges::swap(t1, t2); | |
64 | return t1[0].i == 3*i && t2[0].i == i && t1[1].i == 4*i && t2[1].i == 2*i; | |
65 | } | |
66 | ||
67 | static_assert(check_array_without_adl_swap(1)); | |
68 | ||
69 | ||
70 | constexpr bool check_union_with_adl_swap(int i) | |
71 | { | |
72 | nu::U u1{}, u2{}; | |
73 | u1.i = u2.i = i; | |
74 | std::ranges::swap(u1, u2); | |
75 | return u1.i == 99 && u2.i == 99; | |
76 | } | |
77 | ||
78 | static_assert(check_union_with_adl_swap(1)); |