Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't move from contained value of optional<const T> #2460

Merged
merged 3 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions stl/inc/optional
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,52 @@ struct _Optional_construct_base : _Optional_destruct_base<_Ty> {

template <class _Self>
_CONSTEXPR20 void _Construct_from(_Self&& _Right) noexcept(
is_nothrow_constructible_v<_Ty, decltype((_STD forward<_Self>(_Right)._Value))>) {
is_nothrow_constructible_v<_Ty, decltype(*_STD forward<_Self>(_Right))>) {
// initialize contained value from _Right iff it contains a value
if (_Right._Has_value) {
_Construct(_STD forward<_Self>(_Right)._Value);
_Construct(*_STD forward<_Self>(_Right));
}
}

template <class _Self>
_CONSTEXPR20 void _Assign_from(_Self&& _Right) noexcept(
is_nothrow_constructible_v<_Ty, decltype((_STD forward<_Self>(_Right)._Value))>&&
is_nothrow_assignable_v<_Ty&, decltype((_STD forward<_Self>(_Right)._Value))>) {
is_nothrow_constructible_v<_Ty, decltype(*_STD forward<_Self>(_Right))>&&
is_nothrow_assignable_v<_Ty&, decltype(*_STD forward<_Self>(_Right))>) {
// assign/initialize/destroy contained value from _Right
if (_Right._Has_value) {
_Assign(_STD forward<_Self>(_Right)._Value);
_Assign(*_STD forward<_Self>(_Right));
} else {
this->reset();
}
}

_NODISCARD constexpr _Ty& operator*() & noexcept {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return this->_Value;
}

_NODISCARD constexpr const _Ty& operator*() const& noexcept {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return this->_Value;
}

_NODISCARD constexpr _Ty&& operator*() && noexcept {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return _STD move(this->_Value);
}

_NODISCARD constexpr const _Ty&& operator*() const&& noexcept {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return _STD move(this->_Value);
}
};

template <class _Ty>
Expand Down Expand Up @@ -379,30 +407,7 @@ public:
return _STD addressof(this->_Value);
}

_NODISCARD constexpr const _Ty& operator*() const& noexcept {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return this->_Value;
}
_NODISCARD constexpr _Ty& operator*() & noexcept {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return this->_Value;
}
_NODISCARD constexpr _Ty&& operator*() && noexcept {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return _STD move(this->_Value);
}
_NODISCARD constexpr const _Ty&& operator*() const&& noexcept {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return _STD move(this->_Value);
}
using _Mybase::operator*;

constexpr explicit operator bool() const noexcept {
return this->_Has_value;
Expand Down
43 changes: 43 additions & 0 deletions tests/std/tests/P0220R1_optional/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7382,6 +7382,47 @@ namespace msvc {
(void) t;
}
} // namespace vso614907

namespace gh2458 {
// optional<const meow> o = std::move(lvalue_optional_const_meow);
// was moving from the contained const object
enum class action { none, copy, move };
action last = action::none;

struct Copyable {
Copyable() = default;
Copyable(const Copyable&) {
last = action::copy;
}
Copyable(Copyable&&) {
last = action::move;
}
};

struct ConstMovable {
ConstMovable() = default;
ConstMovable(const ConstMovable&) {
last = action::copy;
}
ConstMovable(const ConstMovable&&) {
last = action::move;
}
};

template <class T, action result>
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
void testMove() {
std::optional<T> orig{std::in_place};
std::optional<T> moved = std::move(orig);
assert(last == result);
}

void run_test() {
testMove<Copyable, action::move>();
testMove<const Copyable, action::copy>();
testMove<ConstMovable, action::move>();
testMove<const ConstMovable, action::move>();
}
} // namespace gh2458
} // namespace msvc

int main() {
Expand Down Expand Up @@ -7478,4 +7519,6 @@ int main() {
msvc::vso406124::run_test();
msvc::vso508126::run_test();
msvc::vso614907::run_test();

msvc::gh2458::run_test();
}