| 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 |
| 22 |
template <class T> |
| 23 |
concept HasValueNoexcept = |
| 24 |
requires(T t) { |
| 25 |
{ t.has_value() } noexcept; |
| 26 |
}; |
| 27 |
|
| 28 |
struct Foo {}; |
| 29 |
static_assert(!HasValueNoexcept<Foo>); |
| 30 |
|
| 31 |
static_assert(HasValueNoexcept<std::expected<int, int>>); |
| 32 |
static_assert(HasValueNoexcept<const std::expected<int, int>>); |
| 33 |
|
| 34 |
constexpr 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 |
|
| 61 |
int main(int, char**) { |
| 62 |
test(); |
| 63 |
static_assert(test()); |
| 64 |
return 0; |
| 65 |
} |