1 | //===----------------------------------------------------------------------===// |
2 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
3 | // See https://llvm.org/LICENSE.txt for license information. |
4 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
5 | // |
6 | //===----------------------------------------------------------------------===// |
7 | |
8 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
9 | |
10 | // constexpr explicit operator bool() const noexcept; |
11 | |
12 | #include <cassert> |
13 | #include <concepts> |
14 | #include <expected> |
15 | #include <type_traits> |
16 | #include <utility> |
17 | |
18 | #include "test_macros.h" |
19 | |
20 | // Test noexcept |
21 | template <class T> |
22 | concept OpBoolNoexcept = |
23 | requires(T t) { |
24 | { static_cast<bool>(t) } noexcept; |
25 | }; |
26 | |
27 | struct Foo {}; |
28 | static_assert(!OpBoolNoexcept<Foo>); |
29 | |
30 | static_assert(OpBoolNoexcept<std::expected<int, int>>); |
31 | static_assert(OpBoolNoexcept<const std::expected<int, int>>); |
32 | |
33 | // Test explicit |
34 | static_assert(!std::is_convertible_v<std::expected<int, int>, bool>); |
35 | |
36 | constexpr bool test() { |
37 | // has_value |
38 | { |
39 | const std::expected<int, int> e(5); |
40 | assert(static_cast<bool>(e)); |
41 | } |
42 | |
43 | // !has_value |
44 | { |
45 | const std::expected<int, int> e(std::unexpect, 5); |
46 | assert(!static_cast<bool>(e)); |
47 | } |
48 | |
49 | return true; |
50 | } |
51 | |
52 | int main(int, char**) { |
53 | test(); |
54 | static_assert(test()); |
55 | return 0; |
56 | } |
57 | |