William Tran-Viet 9f5efd5d03
[libc++] Correct optional<T&> implementation (#174537)
Resolves #174350

- Several issues were found in the current implementation of
`optional<T&>`
- `value()`, `operator*()`, `and_then()`, `transform()`, `operator->()`
still provided their ref-qualified versions for rvalues and `const&`.
- Using the listed methods on an rvalue `optional<T&>` would cause a
compile failure due to a mismatch in return types.
- On the latter, `operator*`, `operator->` would return `const` for a
`optional<T&>`, which is an incorrect deep const.
- A few constructors were missing (`optional<U>&`), and most
constructors relevant to `optional<T&>` were missing `noexcept`
- Constructors and `emplace` were not correctly constructing a `T&` as
specified in _`convert-ref-init-val`_
- Also corrects the behavior of `value_or` which should return
`remove_cv_t<T>` (in our case `decay_t<_Tp>`)
- Add several test cases to verify behavior, update `value_or` tests
2026-01-09 16:33:10 +08:00

117 lines
3.1 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// <optional>
// template<class F> constexpr optional or_else(F&&) &&;
// template<class F> constexpr optional or_else(F&&) const&;
#include "MoveOnly.h"
#include <cassert>
#include <optional>
struct NonMovable {
NonMovable() = default;
NonMovable(NonMovable&&) = delete;
};
template <class Opt, class F>
concept has_or_else = requires(Opt&& opt, F&& f) {
{std::forward<Opt>(opt).or_else(std::forward<F>(f))};
};
template <class T>
std::optional<T> return_optional() {}
static_assert(has_or_else<std::optional<int>&, decltype(return_optional<int>)>);
static_assert(has_or_else<std::optional<int>&&, decltype(return_optional<int>)>);
static_assert(!has_or_else<std::optional<MoveOnly>&, decltype(return_optional<MoveOnly>)>);
static_assert(has_or_else<std::optional<MoveOnly>&&, decltype(return_optional<MoveOnly>)>);
static_assert(!has_or_else<std::optional<NonMovable>&, decltype(return_optional<NonMovable>)>);
static_assert(!has_or_else<std::optional<NonMovable>&&, decltype(return_optional<NonMovable>)>);
std::optional<int> take_int(int) { return 0; }
void take_int_return_void(int) {}
static_assert(!has_or_else<std::optional<int>, decltype(take_int)>);
static_assert(!has_or_else<std::optional<int>, decltype(take_int_return_void)>);
static_assert(!has_or_else<std::optional<int>, int>);
constexpr bool test() {
{
std::optional<int> opt;
assert(opt.or_else([] { return std::optional<int>{0}; }) == 0);
opt = 1;
(void)opt.or_else([] {
assert(false);
return std::optional<int>{};
});
}
{
std::optional<MoveOnly> opt;
opt = std::move(opt).or_else([] { return std::optional<MoveOnly>{MoveOnly{}}; });
(void)std::move(opt).or_else([] {
assert(false);
return std::optional<MoveOnly>{};
});
}
#if TEST_STD_VER >= 26
{
int i = 2;
std::optional<int&> opt;
assert(opt.or_else([&] { return std::optional<int&>{i}; }) == i);
int j = 3;
opt = j;
(void)opt.or_else([] {
assert(false);
return std::optional<int&>{};
});
assert(opt == j);
}
{
int i = 2;
const std::optional<int&> opt;
assert(opt.or_else([&] { return std::optional<int&>{i}; }) == i);
}
{
int i = 2;
std::optional<int&> opt;
assert(std::move(opt).or_else([&] { return std::optional<int&>{i}; }) == i);
int j = 3;
opt = j;
(void)std::move(opt).or_else([] {
assert(false);
return std::optional<int&>{};
});
assert(opt == j);
}
{
int i = 2;
const std::optional<int&> opt = i;
assert(std::move(opt).or_else([] {
assert(false);
return std::optional<int&>{};
}) == i);
}
#endif
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}