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
22template <class T>
23concept OpBoolNoexcept =
24 requires(T t) {
25 { static_cast<bool>(t) } noexcept;
26 };
27
28struct Foo {};
29static_assert(!OpBoolNoexcept<Foo>);
30
31static_assert(OpBoolNoexcept<std::expected<int, int>>);
32static_assert(OpBoolNoexcept<const std::expected<int, int>>);
33
34// Test explicit
35static_assert(!std::is_convertible_v<std::expected<int, int>, bool>);
36
37constexpr 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
53int main(int, char**) {
54 test();
55 static_assert(test());
56 return 0;
57}
58

source code of libcxx/test/std/utilities/expected/expected.expected/observers/bool.pass.cpp