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 | // constexpr explicit operator bool() const noexcept; |
12 | |
13 | #include <cassert> |
14 | #include <concepts> |
15 | #include <expected> |
16 | #include <type_traits> |
17 | #include <utility> |
18 | |
19 | #include "test_macros.h" |
20 | |
21 | // Test noexcept |
22 | template <class T> |
23 | concept OpBoolNoexcept = |
24 | requires(T t) { |
25 | { static_cast<bool>(t) } noexcept; |
26 | }; |
27 | |
28 | struct Foo {}; |
29 | static_assert(!OpBoolNoexcept<Foo>); |
30 | |
31 | static_assert(OpBoolNoexcept<std::expected<int, int>>); |
32 | static_assert(OpBoolNoexcept<const std::expected<int, int>>); |
33 | |
34 | // Test explicit |
35 | static_assert(!std::is_convertible_v<std::expected<int, int>, bool>); |
36 | |
37 | constexpr bool test() { |
38 | // has_value |
39 | { |
40 | const std::expected<int, int> e(5); |
41 | assert(static_cast<bool>(e)); |
42 | } |
43 | |
44 | // !has_value |
45 | { |
46 | const std::expected<int, int> e(std::unexpect, 5); |
47 | assert(!static_cast<bool>(e)); |
48 | } |
49 | |
50 | return true; |
51 | } |
52 | |
53 | int main(int, char**) { |
54 | test(); |
55 | static_assert(test()); |
56 | return 0; |
57 | } |
58 | |