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//
11// constexpr expected();
12
13// Constraints: is_default_constructible_v<T> is true.
14//
15// Effects: Value-initializes val.
16// Postconditions: has_value() is true.
17//
18// Throws: Any exception thrown by the initialization of val.
19
20#include <cassert>
21#include <expected>
22#include <type_traits>
23
24#include "test_macros.h"
25#include "../../types.h"
26
27struct NoDedefaultCtor {
28 NoDedefaultCtor() = delete;
29};
30
31// Test constraints
32static_assert(std::is_default_constructible_v<std::expected<int, int>>);
33static_assert(!std::is_default_constructible_v<std::expected<NoDedefaultCtor, int>>);
34
35struct MyInt {
36 int i;
37 friend constexpr bool operator==(const MyInt&, const MyInt&) = default;
38};
39
40template <class T, class E>
41constexpr void testDefaultCtor() {
42 std::expected<T, E> e;
43 assert(e.has_value());
44 assert(e.value() == T());
45}
46
47template <class T>
48constexpr void testTypes() {
49 testDefaultCtor<T, bool>();
50 testDefaultCtor<T, int>();
51 testDefaultCtor<T, NoDedefaultCtor>();
52}
53
54constexpr bool test() {
55 testTypes<int>();
56 testTypes<MyInt>();
57 testTypes<TailClobberer<0>>();
58 return true;
59}
60
61void testException() {
62#ifndef TEST_HAS_NO_EXCEPTIONS
63 struct Throwing {
64 Throwing() { throw Except{}; };
65 };
66
67 try {
68 std::expected<Throwing, int> u;
69 assert(false);
70 } catch (Except) {
71 }
72#endif // TEST_HAS_NO_EXCEPTIONS
73}
74
75int main(int, char**) {
76 test();
77 static_assert(test());
78 testException();
79 return 0;
80}
81

source code of libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.default.pass.cpp