Skip to content

Commit 9f57c4c

Browse files
committed
Deduct
1 parent 259b8f9 commit 9f57c4c

File tree

1 file changed

+42
-40
lines changed
  • libcxx/test/std/utilities/optional/optional.object/optional.object.ctor

1 file changed

+42
-40
lines changed

libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.pass.cpp

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,75 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
// REQUIRES: std-at-least-c++17
10+
911
// <optional>
10-
// UNSUPPORTED: c++03, c++11, c++14
1112

1213
// template<class T>
1314
// optional(T) -> optional<T>;
1415

15-
#include <optional>
1616
#include <cassert>
17+
#include <optional>
1718

1819
#include "test_macros.h"
1920

20-
struct A {};
21+
struct A {
22+
friend constexpr bool operator==(const A&, const A&) { return true; }
23+
};
2124

22-
int main(int, char**)
23-
{
24-
// Test the explicit deduction guides
25-
{
26-
// optional(T)
27-
std::optional opt(5);
28-
ASSERT_SAME_TYPE(decltype(opt), std::optional<int>);
29-
assert(static_cast<bool>(opt));
30-
assert(*opt == 5);
31-
}
25+
template <typename T>
26+
constexpr void test_deduct(T arg) {
27+
std::optional opt(arg);
3228

33-
{
34-
// optional(T)
35-
std::optional opt(A{});
36-
ASSERT_SAME_TYPE(decltype(opt), std::optional<A>);
37-
assert(static_cast<bool>(opt));
38-
}
29+
ASSERT_SAME_TYPE(decltype(opt), std::optional<T>);
30+
assert(static_cast<bool>(opt));
31+
assert(*opt == arg);
32+
}
3933

40-
{
41-
// optional(const T&);
34+
constexpr bool test() {
35+
// optional(T)
36+
test_deduct<int>(5);
37+
test_deduct<A>(A{});
38+
39+
{
40+
// optional(const T&);
4241
const int& source = 5;
43-
std::optional opt(source);
44-
ASSERT_SAME_TYPE(decltype(opt), std::optional<int>);
45-
assert(static_cast<bool>(opt));
46-
assert(*opt == 5);
47-
}
42+
test_deduct<int>(source);
43+
}
4844

49-
{
50-
// optional(T*);
45+
{
46+
// optional(T*);
5147
const int* source = nullptr;
52-
std::optional opt(source);
53-
ASSERT_SAME_TYPE(decltype(opt), std::optional<const int*>);
54-
assert(static_cast<bool>(opt));
55-
assert(*opt == nullptr);
56-
}
48+
test_deduct<const int*>(source);
49+
}
5750

58-
{
59-
// optional(T[]);
51+
{
52+
// optional(T[]);
6053
int source[] = {1, 2, 3};
6154
std::optional opt(source);
55+
6256
ASSERT_SAME_TYPE(decltype(opt), std::optional<int*>);
6357
assert(static_cast<bool>(opt));
6458
assert((*opt)[0] == 1);
65-
}
59+
}
6660

67-
// Test the implicit deduction guides
68-
{
69-
// optional(optional);
61+
// Test the implicit deduction guides
62+
{
63+
// optional(optional);
7064
std::optional<char> source('A');
7165
std::optional opt(source);
66+
7267
ASSERT_SAME_TYPE(decltype(opt), std::optional<char>);
7368
assert(static_cast<bool>(opt) == static_cast<bool>(source));
7469
assert(*opt == *source);
75-
}
70+
}
71+
72+
return true;
73+
}
74+
75+
int main(int, char**) {
76+
test();
77+
static_assert(test());
7678

7779
return 0;
7880
}

0 commit comments

Comments
 (0)