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 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 10 11 // friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))); 12 // 13 // Constraints: is_swappable_v<E> is true. 14 // 15 // Effects: Equivalent to x.swap(y). 16 17 #include <cassert> 18 #include <concepts> 19 #include <expected> 20 #include <type_traits> 21 #include <utility> 22 23 // test noexcept 24 struct NoexceptSwap { 25 friend void swap(NoexceptSwap&, NoexceptSwap&) noexcept; 26 }; 27 28 struct MayThrowSwap { 29 friend void swap(MayThrowSwap&, MayThrowSwap&); 30 }; 31 32 template <class T> 33 concept ADLSwapNoexcept = 34 requires(T& t1, T& t2) { 35 { swap(t1, t2) } noexcept; 36 }; 37 38 static_assert(ADLSwapNoexcept<std::unexpected<NoexceptSwap>>); 39 static_assert(!ADLSwapNoexcept<std::unexpected<MayThrowSwap>>); 40 41 // test constraint 42 struct NonSwappable { 43 NonSwappable& operator=(const NonSwappable&) = delete; 44 }; 45 46 static_assert(std::is_swappable_v<std::unexpected<int>>); 47 static_assert(std::is_swappable_v<std::unexpected<MayThrowSwap>>); 48 static_assert(!std::is_swappable_v<std::unexpected<NonSwappable>>); 49 50 struct ADLSwap { 51 constexpr ADLSwap(int ii) : i(ii) {} 52 ADLSwap& operator=(const ADLSwap&) = delete; 53 int i; 54 constexpr friend void swap(ADLSwap& x, ADLSwap& y) { std::swap(x.i, y.i); } 55 }; 56 57 constexpr bool test() { 58 std::unexpected<ADLSwap> unex1(5); 59 std::unexpected<ADLSwap> unex2(6); 60 swap(unex1, unex2); 61 assert(unex1.error().i == 6); 62 assert(unex2.error().i == 5); 63 return true; 64 } 65 66 int main(int, char**) { 67 test(); 68 static_assert(test()); 69 return 0; 70 } 71