diff mbox series

[2/2] libstdc++: Use _M_get() in std::optional internals

Message ID 20240724195050.4006283-2-jwakely@redhat.com
State New
Headers show
Series [1/2] libstdc++: Move std::optional assertions out of _M_get() | expand

Commit Message

Jonathan Wakely July 24, 2024, 7:48 p.m. UTC
Tested x86_64-linux.

-- >8 --

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<U> 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<U> as friend.
	(optional(const optional<U>&)): Use
	_M_get instead of operator*.
	(optional(optional<U>&&)): Likewise.
	(operator=(const optional<U>&)): Likewise.
	(operator=(optional<U>&&)): Likewise.
	(and_then, tansform): Likewise.
---
 libstdc++-v3/include/std/optional | 38 ++++++++++++++++---------------
 1 file changed, 20 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional
index af72004645e..9ed7ab50140 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<typename _Up,
@@ -773,7 +773,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
 	{
 	  if (__t)
-	    emplace(*__t);
+	    emplace(__t._M_get());
 	}
 
       template<typename _Up,
@@ -786,7 +786,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	noexcept(is_nothrow_constructible_v<_Tp, _Up>)
 	{
 	  if (__t)
-	    emplace(std::move(*__t));
+	    emplace(std::move(__t._M_get()));
 	}
 
       template<typename _Up,
@@ -799,7 +799,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	noexcept(is_nothrow_constructible_v<_Tp, _Up>)
 	{
 	  if (__t)
-	    emplace(std::move(*__t));
+	    emplace(std::move(__t._M_get()));
 	}
 
       template<typename... _Args,
@@ -863,9 +863,9 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  if (__u)
 	    {
 	      if (this->_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<typename _Fn>
@@ -1068,7 +1068,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 			"the function passed to std::optional<T>::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<T>::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<T>::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<T>::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<invoke_result_t<_Fn, _Tp&>>;
 	  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<invoke_result_t<_Fn, const _Tp&>>;
 	  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<invoke_result_t<_Fn, _Tp>>;
 	  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<invoke_result_t<_Fn, const _Tp>>;
 	  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<typename _Up> friend class optional;
 
+#if __cpp_lib_optional >= 202110L // C++23
       template<typename _Fn, typename _Value>
 	explicit constexpr
 	optional(_Optional_func<_Fn> __f, _Value&& __v)