Skip to content

Commit

Permalink
[libc++][test] Mark optional test functions as `TEST_CONSTEXPR_CXX2…
Browse files Browse the repository at this point in the history
…0` (#94172)

[P2231R1](https://wg21.link/P2231R1) "Missing `constexpr` in
`std::optional` and `std::variant`" was accepted as a C++20 Defect
Report, not a C++17 Defect Report. Accordingly, `test_empty_emplace()`
and `check_reset()` should be marked as `TEST_CONSTEXPR_CXX20`. Note
that their `static_assert`s are properly guarded:


https://github.com/llvm/llvm-project/blob/4ce65423be0ba1d90c11b6a79981d6314e1cf36d/libcxx/test/std/utilities/optional/optional.object/optional.object.assign/emplace.pass.cpp#L270-L272


https://github.com/llvm/llvm-project/blob/4ce65423be0ba1d90c11b6a79981d6314e1cf36d/libcxx/test/std/utilities/optional/optional.object/optional.object.mod/reset.pass.cpp#L53-L55

Found while running libc++'s tests with MSVC's STL, as we activate our
`constexpr` here for C++20 and above.
  • Loading branch information
StephanTLavavej authored Jun 3, 2024
1 parent f4a7f81 commit 84742cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,15 @@ void test_on_test_type() {
}
}

constexpr bool test_empty_emplace()
{
optional<const int> opt;
auto &v = opt.emplace(42);
static_assert( std::is_same_v<const int&, decltype(v)>, "" );
assert(*opt == 42);
assert( v == 42);
opt.emplace();
assert(*opt == 0);
return true;
TEST_CONSTEXPR_CXX20 bool test_empty_emplace() {
optional<const int> opt;
auto& v = opt.emplace(42);
static_assert(std::is_same_v<const int&, decltype(v)>, "");
assert(*opt == 42);
assert(v == 42);
opt.emplace();
assert(*opt == 0);
return true;
}

int main(int, char**)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ struct X

bool X::dtor_called = false;

constexpr bool check_reset()
{
{
optional<int> opt;
static_assert(noexcept(opt.reset()) == true, "");
opt.reset();
assert(static_cast<bool>(opt) == false);
}
{
optional<int> opt(3);
opt.reset();
assert(static_cast<bool>(opt) == false);
}
return true;
TEST_CONSTEXPR_CXX20 bool check_reset() {
{
optional<int> opt;
static_assert(noexcept(opt.reset()) == true, "");
opt.reset();
assert(static_cast<bool>(opt) == false);
}
{
optional<int> opt(3);
opt.reset();
assert(static_cast<bool>(opt) == false);
}
return true;
}

int main(int, char**)
Expand Down

0 comments on commit 84742cd

Please sign in to comment.