Skip to content

LWG-2762 unique_ptr operator*() should be noexcept #2257

@AlexGuteniev

Description

@AlexGuteniev

LWG-2762 asks for unique_ptr and optional indirection operators to be noexcept

unique_ptr is already noexcept. It is marked as /* strengthened */ as this noexcept was not previously require by the Standard.

Note that LWG requires conditional noexcept(noexcept(*declval<pointer>()))

According to [unique.ptr.single.general]/3, unique_ptr<T, D> may obtain fancy pointer as underlying pointer type via remove_­reference_­t<D>​::​pointer.

_NODISCARD add_lvalue_reference_t<_Ty> operator*() const noexcept /* strengthened */ {

So the fix is to change noexcept to conditional, and remove /* strengthened */ comment.

optional misses noexcept, it should be added:

STL/stl/inc/optional

Lines 345 to 381 in dc888f7

_NODISCARD constexpr const _Ty* operator->() const {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return _STD addressof(this->_Value);
}
_NODISCARD constexpr _Ty* operator->() {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(this->_Has_value, "Cannot access value of empty optional");
#endif // _CONTAINER_DEBUG_LEVEL > 0
return _STD addressof(this->_Value);
}
_NODISCARD constexpr const _Ty& operator*() const& {
#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*() & {
#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*() && {
#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&& {
#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);
}

These are unconditional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LWGLibrary Working Group issuefixedSomething works now, yay!good first issueGood for newcomers

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions