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 bool has_value() 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#include "../../types.h"
20
21// Test noexcept
22template <class T>
23concept HasValueNoexcept =
24 requires(T t) {
25 { t.has_value() } noexcept;
26 };
27
28struct Foo {};
29static_assert(!HasValueNoexcept<Foo>);
30
31static_assert(HasValueNoexcept<std::expected<int, int>>);
32static_assert(HasValueNoexcept<const std::expected<int, int>>);
33
34constexpr bool test() {
35 // has_value
36 {
37 const std::expected<void, int> e;
38 assert(e.has_value());
39 }
40
41 // !has_value
42 {
43 const std::expected<void, int> e(std::unexpect, 5);
44 assert(!e.has_value());
45 }
46
47 // See comments of the corresponding test in
48 // "expected.expected/observers/has_value.pass.cpp".
49 {
50 const std::expected<void, TailClobberer<1>> e(std::unexpect);
51 // clang-cl does not support [[no_unique_address]] yet.
52#if !(defined(TEST_COMPILER_CLANG) && defined(_MSC_VER))
53 LIBCPP_STATIC_ASSERT(sizeof(TailClobberer<1>) == sizeof(e));
54#endif
55 assert(!e.has_value());
56 }
57
58 return true;
59}
60
61int main(int, char**) {
62 test();
63 static_assert(test());
64 return 0;
65}
66

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