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

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