brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.2 KiB · cca3ec5 Raw
138 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// template<class U, class G>12//   constexpr explicit(!is_convertible_v<const G&, E>) expected(const expected<U, G>& rhs);13//14// Let GF be const G&15//16// Constraints:17// - is_void_v<U> is true; and18// - is_constructible_v<E, GF> is true; and19// - is_constructible_v<unexpected<E>, expected<U, G>&> is false; and20// - is_constructible_v<unexpected<E>, expected<U, G>> is false; and21// - is_constructible_v<unexpected<E>, const expected<U, G>&> is false; and22// - is_constructible_v<unexpected<E>, const expected<U, G>> is false.23//24// Effects: If rhs.has_value() is false, direct-non-list-initializes unex with std::forward<GF>(rhs.error()).25//26// Postconditions: rhs.has_value() is unchanged; rhs.has_value() == this->has_value() is true.27//28// Throws: Any exception thrown by the initialization of unex.29 30#include <cassert>31#include <concepts>32#include <expected>33#include <type_traits>34#include <utility>35 36#include "test_macros.h"37#include "../../types.h"38 39// Test Constraints:40template <class T1, class Err1, class T2, class Err2>41concept canCstrFromExpected = std::is_constructible_v<std::expected<T1, Err1>, const std::expected<T2, Err2>&>;42 43struct CtorFromInt {44  CtorFromInt(int);45};46 47static_assert(canCstrFromExpected<void, CtorFromInt, void, int>);48 49struct NoCtorFromInt {};50 51// !is_void_v<E>52static_assert(!canCstrFromExpected<void, int, int, int>);53 54// !is_constructible_v<E, GF>55static_assert(!canCstrFromExpected<void, NoCtorFromInt, void, int>);56 57template <class T>58struct CtorFrom {59  explicit CtorFrom(int)60    requires(!std::same_as<T, int>);61  explicit CtorFrom(T);62  explicit CtorFrom(auto&&) = delete;63};64 65// Note for below 4 tests, because their E is constructible from cvref of std::expected<void, int>,66// unexpected<E> will be constructible from cvref of std::expected<void, int>67// is_constructible_v<unexpected<E>, expected<U, G>&>68static_assert(!canCstrFromExpected<void, CtorFrom<std::expected<void, int>&>, void, int>);69 70// is_constructible_v<unexpected<E>, expected<U, G>>71static_assert(!canCstrFromExpected<void, CtorFrom<std::expected<void, int>&&>, void, int>);72 73// is_constructible_v<unexpected<E>, const expected<U, G>&> is false74static_assert(!canCstrFromExpected<void, CtorFrom<std::expected<void, int> const&>, void, int>);75 76// is_constructible_v<unexpected<E>, const expected<U, G>>77static_assert(!canCstrFromExpected<void, CtorFrom<std::expected<void, int> const&&>, void, int>);78 79// test explicit80static_assert(std::is_convertible_v<const std::expected<void, int>&, std::expected<void, long>>);81 82// !is_convertible_v<GF, E>.83static_assert(std::is_constructible_v<std::expected<void, CtorFrom<int>>, const std::expected<void, int>&>);84static_assert(!std::is_convertible_v<const std::expected<void, int>&, std::expected<void, CtorFrom<int>>>);85 86struct Data {87  int i;88  constexpr Data(int ii) : i(ii) {}89};90 91constexpr bool test() {92  // convert the error93  {94    const std::expected<void, int> e1(std::unexpect, 5);95    std::expected<void, Data> e2 = e1;96    assert(!e2.has_value());97    assert(e2.error().i == 5);98    assert(!e1.has_value());99    assert(e1.error() == 5);100  }101 102  // convert TailClobberer103  {104    const std::expected<void, TailClobbererNonTrivialMove<1>> e1(std::unexpect);105    std::expected<void, TailClobberer<1>> e2 = e1;106    assert(!e2.has_value());107    assert(!e1.has_value());108  }109 110  return true;111}112 113void testException() {114#ifndef TEST_HAS_NO_EXCEPTIONS115  struct ThrowingInt {116    ThrowingInt(int) { throw Except{}; }117  };118 119  // throw on converting error120  {121    const std::expected<void, int> e1(std::unexpect);122    try {123      [[maybe_unused]] std::expected<void, ThrowingInt> e2 = e1;124      assert(false);125    } catch (Except) {126    }127  }128 129#endif // TEST_HAS_NO_EXCEPTIONS130}131 132int main(int, char**) {133  test();134  static_assert(test());135  testException();136  return 0;137}138