Skip to content

Commit 41a716e

Browse files
committed
in_place_t
1 parent cde8298 commit 41a716e

File tree

1 file changed

+53
-112
lines changed

1 file changed

+53
-112
lines changed

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

Lines changed: 53 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -6,144 +6,85 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// UNSUPPORTED: c++03, c++11, c++14
9+
// REQUIRED: std-at-least-c++17
1010

1111
// <optional>
1212

1313
// template <class... Args>
1414
// constexpr explicit optional(in_place_t, Args&&... args);
1515

16+
#include <cassert>
1617
#include <optional>
1718
#include <type_traits>
18-
#include <cassert>
19+
#include <utility>
1920

2021
#include "test_macros.h"
2122

22-
using std::optional;
23-
using std::in_place_t;
2423
using std::in_place;
24+
using std::in_place_t;
25+
using std::optional;
2526

26-
class X
27-
{
28-
int i_;
29-
int j_ = 0;
30-
public:
31-
X() : i_(0) {}
32-
X(int i) : i_(i) {}
33-
X(int i, int j) : i_(i), j_(j) {}
34-
35-
~X() {}
36-
37-
friend bool operator==(const X& x, const X& y)
38-
{return x.i_ == y.i_ && x.j_ == y.j_;}
39-
};
27+
class X {
28+
int i_;
29+
int j_ = 0;
4030

41-
class Y
42-
{
43-
int i_;
44-
int j_ = 0;
4531
public:
46-
constexpr Y() : i_(0) {}
47-
constexpr Y(int i) : i_(i) {}
48-
constexpr Y(int i, int j) : i_(i), j_(j) {}
32+
constexpr X() : i_(0) {}
33+
constexpr X(int i) : i_(i) {}
34+
constexpr X(int i, int j) : i_(i), j_(j) {}
4935

50-
friend constexpr bool operator==(const Y& x, const Y& y)
51-
{return x.i_ == y.i_ && x.j_ == y.j_;}
36+
~X() = default;
37+
38+
friend constexpr bool operator==(const X& x, const X& y) { return x.i_ == y.i_ && x.j_ == y.j_; }
5239
};
5340

54-
class Z
55-
{
41+
class Z {
5642
public:
57-
Z(int) {TEST_THROW(6);}
43+
Z(int) { TEST_THROW(6); }
5844
};
5945

46+
template <typename T, typename... Args>
47+
constexpr void test_inplace(Args... args) {
48+
optional<T> opt(in_place, args...);
49+
assert(bool(opt));
50+
assert(*opt == T(args...));
6051

61-
int main(int, char**)
62-
{
63-
{
64-
constexpr optional<int> opt(in_place, 5);
65-
static_assert(static_cast<bool>(opt) == true, "");
66-
static_assert(*opt == 5, "");
67-
68-
struct test_constexpr_ctor
69-
: public optional<int>
70-
{
71-
constexpr test_constexpr_ctor(in_place_t, int i)
72-
: optional<int>(in_place, i) {}
73-
};
74-
75-
}
76-
{
77-
optional<const int> opt(in_place, 5);
78-
assert(*opt == 5);
79-
}
80-
{
81-
const optional<X> opt(in_place);
82-
assert(static_cast<bool>(opt) == true);
83-
assert(*opt == X());
84-
}
85-
{
86-
const optional<X> opt(in_place, 5);
87-
assert(static_cast<bool>(opt) == true);
88-
assert(*opt == X(5));
89-
}
90-
{
91-
const optional<X> opt(in_place, 5, 4);
92-
assert(static_cast<bool>(opt) == true);
93-
assert(*opt == X(5, 4));
94-
}
95-
{
96-
constexpr optional<Y> opt(in_place);
97-
static_assert(static_cast<bool>(opt) == true, "");
98-
static_assert(*opt == Y(), "");
99-
100-
struct test_constexpr_ctor
101-
: public optional<Y>
102-
{
103-
constexpr test_constexpr_ctor(in_place_t)
104-
: optional<Y>(in_place) {}
105-
};
106-
107-
}
108-
{
109-
constexpr optional<Y> opt(in_place, 5);
110-
static_assert(static_cast<bool>(opt) == true, "");
111-
static_assert(*opt == Y(5), "");
112-
113-
struct test_constexpr_ctor
114-
: public optional<Y>
115-
{
116-
constexpr test_constexpr_ctor(in_place_t, int i)
117-
: optional<Y>(in_place, i) {}
118-
};
119-
120-
}
121-
{
122-
constexpr optional<Y> opt(in_place, 5, 4);
123-
static_assert(static_cast<bool>(opt) == true, "");
124-
static_assert(*opt == Y(5, 4), "");
125-
126-
struct test_constexpr_ctor
127-
: public optional<Y>
128-
{
129-
constexpr test_constexpr_ctor(in_place_t, int i, int j)
130-
: optional<Y>(in_place, i, j) {}
131-
};
52+
struct test_constexpr_ctor : public optional<int> {
53+
constexpr test_constexpr_ctor(in_place_t, int i) : optional<int>(in_place, i) {}
54+
};
55+
}
13256

133-
}
57+
TEST_CONSTEXPR_CXX26 void test_throwing() {
13458
#ifndef TEST_HAS_NO_EXCEPTIONS
135-
{
136-
try
137-
{
138-
const optional<Z> opt(in_place, 1);
139-
assert(false);
140-
}
141-
catch (int i)
142-
{
143-
assert(i == 6);
144-
}
59+
{
60+
try {
61+
const optional<Z> opt(in_place, 1);
62+
assert(false);
63+
} catch (int i) {
64+
assert(i == 6);
14565
}
66+
}
14667
#endif
68+
}
69+
70+
constexpr bool test() {
71+
test_inplace<int>(5);
72+
test_inplace<const int>(5);
73+
test_inplace<X>();
74+
test_inplace<X>(5);
75+
test_inplace<X>(5, 4);
76+
#if TEST_STD_VER >= 26 && 0
77+
test_throwing();
78+
#endif
79+
return true;
80+
}
81+
82+
int main(int, char**) {
83+
test();
84+
static_assert(test());
14785

86+
{
87+
test_throwing();
88+
}
14889
return 0;
14990
}

0 commit comments

Comments
 (0)