diff options
author | yronglin <yronglin777@gmail.com> | 2023-05-18 01:05:13 +0800 |
---|---|---|
committer | yronglin <yronglin777@gmail.com> | 2023-05-18 01:05:25 +0800 |
commit | acce2a315945e386a7be6f014ebe90c2a28f38d9 (patch) | |
tree | 4dc99806d34604023905d11f645690782afe0a66 /libcxx/test/libcxx/utilities/expected/expected.void/error_or.mandates.verify.cpp | |
parent | 5ae9098e5c1564bdfe0ae0fdbc02e581138eeac7 (diff) | |
download | llvm-acce2a315945e386a7be6f014ebe90c2a28f38d9.tar.gz |
[libc++] Implement P2505R5(Monadic operations for std::expected).
Implement P2505R5(https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2505r5.html)
Reviewed By: #libc, philnik, ldionne
Differential Revision: https://reviews.llvm.org/D140911
Diffstat (limited to 'libcxx/test/libcxx/utilities/expected/expected.void/error_or.mandates.verify.cpp')
-rw-r--r-- | libcxx/test/libcxx/utilities/expected/expected.void/error_or.mandates.verify.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/libcxx/test/libcxx/utilities/expected/expected.void/error_or.mandates.verify.cpp b/libcxx/test/libcxx/utilities/expected/expected.void/error_or.mandates.verify.cpp new file mode 100644 index 000000000000..a36665b25c87 --- /dev/null +++ b/libcxx/test/libcxx/utilities/expected/expected.void/error_or.mandates.verify.cpp @@ -0,0 +1,71 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// Test the mandates +// template<class G = E> constexpr E error_or(G&&) const &; +// Mandates: is_copy_constructible_v<G> is true and is_convertible_v<U, E> is true. + +// template<class G = E> constexpr E error_or(G&&) &&; +// Mandates: is_move_constructible_v<G> is true and is_convertible_v<U, E> is true. + +#include <expected> +#include <utility> + +struct NonCopyable { + NonCopyable(int) {} + NonCopyable(const NonCopyable&) = delete; +}; + +struct NonMovable { + NonMovable(int) {} + NonMovable(NonMovable&&) = delete; +}; + +struct NotConvertibleFromInt {}; + +// clang-format off +void test() { + // const & overload + // !is_copy_constructible_v<G>, + { + const std::expected<void, NonCopyable> f1(std::unexpect, 0); + f1.error_or(5); // expected-note{{in instantiation of function template specialization 'std::expected<void, NonCopyable>::error_or<int>' requested here}} + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}error_type has to be copy constructible}} + // expected-error-re@*:* {{call to deleted constructor of{{.*}}}} + } + + // const & overload + // !is_convertible_v<U, T> + { + const std::expected<void, NotConvertibleFromInt> f1(std::unexpect, NotConvertibleFromInt{}); + f1.error_or(5); // expected-note{{in instantiation of function template specialization 'std::expected<void, NotConvertibleFromInt>::error_or<int>' requested here}} + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}argument has to be convertible to error_type}} + // expected-error-re@*:* {{no viable conversion from returned value of type{{.*}}}} + } + + // && overload + // !is_move_constructible_v<T>, + { + std::expected<void, NonMovable> f1(std::unexpect, 0); + std::move(f1).error_or(5); // expected-note{{in instantiation of function template specialization 'std::expected<void, NonMovable>::error_or<int>' requested here}} + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}error_type has to be move constructible}} + // expected-error-re@*:* {{call to deleted constructor of{{.*}}}} + } + + // && overload + // !is_convertible_v<U, T> + { + std::expected<void, NotConvertibleFromInt> f1(std::unexpect, NotConvertibleFromInt{}); + std::move(f1).error_or(5); // expected-note{{in instantiation of function template specialization 'std::expected<void, NotConvertibleFromInt>::error_or<int>' requested here}} + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}argument has to be convertible to error_type}} + // expected-error-re@*:* {{no viable conversion from returned value of type{{.*}}}} + } +} +// clang-format on |