brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.8 KiB · 711a779 Raw
71 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 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 noexcept24struct NoexceptSwap {25  friend void swap(NoexceptSwap&, NoexceptSwap&) noexcept;26};27 28struct MayThrowSwap {29  friend void swap(MayThrowSwap&, MayThrowSwap&);30};31 32template <class T>33concept ADLSwapNoexcept =34    requires(T& t1, T& t2) {35      { swap(t1, t2) } noexcept;36    };37 38static_assert(ADLSwapNoexcept<std::unexpected<NoexceptSwap>>);39static_assert(!ADLSwapNoexcept<std::unexpected<MayThrowSwap>>);40 41// test constraint42struct NonSwappable {43  NonSwappable& operator=(const NonSwappable&) = delete;44};45 46static_assert(std::is_swappable_v<std::unexpected<int>>);47static_assert(std::is_swappable_v<std::unexpected<MayThrowSwap>>);48static_assert(!std::is_swappable_v<std::unexpected<NonSwappable>>);49 50struct 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 57constexpr 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 66int main(int, char**) {67  test();68  static_assert(test());69  return 0;70}71