58 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 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 noexcept22template <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<void, int>>);32static_assert(OpBoolNoexcept<const std::expected<void, int>>);33 34// Test explicit35static_assert(!std::is_convertible_v<std::expected<void, int>, bool>);36 37constexpr bool test() {38 // has_value39 {40 const std::expected<void, int> e;41 assert(static_cast<bool>(e));42 }43 44 // !has_value45 {46 const std::expected<void, 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