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