From: Jonathan Wakely Date: Tue, 23 Jul 2024 10:46:05 +0000 (+0100) Subject: libstdc++: Use _M_get() in std::optional internals X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=13c94d827596cb90c53e35e0c6839e10c0944d25;p=thirdparty%2Fgcc.git libstdc++: Use _M_get() in std::optional internals Now that _base::_M_get() doesn't check the precondition, we can use _M_get() instead of operator*() for the internal uses where we've already checked the precondition holds. Add a using-declaration so that we don't need to lookup _M_get in the dependent base class, and make optional a friend so that the converting constructors and assignment operators can use the parameter's _M_get member. libstdc++-v3/ChangeLog: * include/std/optional (optional): Add using-declaraction for _Base::_M_get and declare optional as friend. (optional(const optional&)): Use _M_get instead of operator*. (optional(optional&&)): Likewise. (operator=(const optional&)): Likewise. (operator=(optional&&)): Likewise. (and_then, tansform): Likewise. --- diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index af72004645e2..9ed7ab501402 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -760,7 +760,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION noexcept(is_nothrow_constructible_v<_Tp, const _Up&>) { if (__t) - emplace(*__t); + emplace(__t._M_get()); } template) { if (__t) - emplace(*__t); + emplace(__t._M_get()); } template) { if (__t) - emplace(std::move(*__t)); + emplace(std::move(__t._M_get())); } template) { if (__t) - emplace(std::move(*__t)); + emplace(std::move(__t._M_get())); } template_M_is_engaged()) - this->_M_get() = *__u; + this->_M_get() = __u._M_get(); else - this->_M_construct(*__u); + this->_M_construct(__u._M_get()); } else { @@ -889,9 +889,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__u) { if (this->_M_is_engaged()) - this->_M_get() = std::move(*__u); + this->_M_get() = std::move(__u._M_get()); else - this->_M_construct(std::move(*__u)); + this->_M_construct(std::move(__u._M_get())); } else { @@ -1056,7 +1056,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return static_cast<_Tp>(std::forward<_Up>(__u)); } -#if __cpp_lib_optional >= 202110L +#if __cpp_lib_optional >= 202110L // C++23 // [optional.monadic] template @@ -1068,7 +1068,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION "the function passed to std::optional::and_then " "must return a std::optional"); if (has_value()) - return std::__invoke(std::forward<_Fn>(__f), **this); + return std::__invoke(std::forward<_Fn>(__f), _M_get()); else return _Up(); } @@ -1082,7 +1082,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION "the function passed to std::optional::and_then " "must return a std::optional"); if (has_value()) - return std::__invoke(std::forward<_Fn>(__f), **this); + return std::__invoke(std::forward<_Fn>(__f), _M_get()); else return _Up(); } @@ -1096,7 +1096,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION "the function passed to std::optional::and_then " "must return a std::optional"); if (has_value()) - return std::__invoke(std::forward<_Fn>(__f), std::move(**this)); + return std::__invoke(std::forward<_Fn>(__f), std::move(_M_get())); else return _Up(); } @@ -1110,7 +1110,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION "the function passed to std::optional::and_then " "must return a std::optional"); if (has_value()) - return std::__invoke(std::forward<_Fn>(__f), std::move(**this)); + return std::__invoke(std::forward<_Fn>(__f), std::move(_M_get())); else return _Up(); } @@ -1121,7 +1121,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { using _Up = remove_cv_t>; if (has_value()) - return optional<_Up>(_Optional_func<_Fn>{__f}, **this); + return optional<_Up>(_Optional_func<_Fn>{__f}, _M_get()); else return optional<_Up>(); } @@ -1132,7 +1132,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { using _Up = remove_cv_t>; if (has_value()) - return optional<_Up>(_Optional_func<_Fn>{__f}, **this); + return optional<_Up>(_Optional_func<_Fn>{__f}, _M_get()); else return optional<_Up>(); } @@ -1143,7 +1143,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { using _Up = remove_cv_t>; if (has_value()) - return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this)); + return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(_M_get())); else return optional<_Up>(); } @@ -1154,7 +1154,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { using _Up = remove_cv_t>; if (has_value()) - return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this)); + return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(_M_get())); else return optional<_Up>(); } @@ -1193,9 +1193,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _GLIBCXX20_CONSTEXPR void reset() noexcept { this->_M_reset(); } private: -#if __cplusplus >= 202002L + using _Base::_M_get; + template friend class optional; +#if __cpp_lib_optional >= 202110L // C++23 template explicit constexpr optional(_Optional_func<_Fn> __f, _Value&& __v)