public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r11-9297] libstdc++: Simplify std::optional::value()
Date: Wed, 24 Nov 2021 11:51:59 +0000 (GMT)	[thread overview]
Message-ID: <20211124115159.392D5385780D@sourceware.org> (raw)

https://gcc.gnu.org/g:20cd18fc47a4e45d8de9710c8ab78188e2638f08

commit r11-9297-g20cd18fc47a4e45d8de9710c8ab78188e2638f08
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jul 27 14:50:28 2021 +0100

    libstdc++: Simplify std::optional::value()
    
    The structure of these functions likely dates from the time before G++
    fully supported C++14 extended constexpr, so that the throw expression
    had to be the operand of a conditional expression. That is not true now,
    so we can use a more straightforward version of the code.
    
    We can also simplify the declaration of __throw_bad_optional_access by
    using the C++11-style [[noreturn]] attribute so that a separate
    declaration isn't needed.
    
    libstdc++-v3/ChangeLog:
    
            * include/experimental/optional (__throw_bad_optional_access):
            Replace GNU attribute with C++11 attribute.
            (optional::value, optional::value_or): Use if statements
            instead of conditional expressions.
            * include/std/optional (__throw_bad_optional_access)
            (optional::value, optional::value_or): Likewise.
    
    (cherry picked from commit 9360d6cd1706882dfffd3c7a08b5956c37207a11)

Diff:
---
 libstdc++-v3/include/experimental/optional | 56 +++++++++++++-----------------
 libstdc++-v3/include/std/optional          | 43 +++++++++++------------
 2 files changed, 46 insertions(+), 53 deletions(-)

diff --git a/libstdc++-v3/include/experimental/optional b/libstdc++-v3/include/experimental/optional
index ae2418f0500..c1d61d6a0d7 100644
--- a/libstdc++-v3/include/experimental/optional
+++ b/libstdc++-v3/include/experimental/optional
@@ -114,12 +114,8 @@ inline namespace fundamentals_v1
 
   /// @cond undocumented
 
-  void
-  __throw_bad_optional_access(const char*)
-  __attribute__((__noreturn__));
-
   // XXX Does not belong here.
-  inline void
+  [[noreturn]] inline void
   __throw_bad_optional_access(const char* __s)
   { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
 
@@ -674,41 +670,37 @@ inline namespace fundamentals_v1
       constexpr const _Tp&
       value() const&
       {
-	return this->_M_is_engaged()
-	  ?  this->_M_get()
-	  : (__throw_bad_optional_access("Attempt to access value of a "
-		                         "disengaged optional object"),
-	     this->_M_get());
+	if (this->_M_is_engaged())
+	  return this->_M_get();
+	__throw_bad_optional_access("Attempt to access value of a "
+				    "disengaged optional object");
       }
 
       constexpr _Tp&
       value()&
       {
-	return this->_M_is_engaged()
-	  ?  this->_M_get()
-	  : (__throw_bad_optional_access("Attempt to access value of a "
-		                         "disengaged optional object"),
-	     this->_M_get());
+	if (this->_M_is_engaged())
+	  return this->_M_get();
+	__throw_bad_optional_access("Attempt to access value of a "
+				    "disengaged optional object");
       }
 
       constexpr _Tp&&
       value()&&
       {
-	return this->_M_is_engaged()
-	  ?  std::move(this->_M_get())
-	  : (__throw_bad_optional_access("Attempt to access value of a "
-		                         "disengaged optional object"),
-	     std::move(this->_M_get()));
+	if (this->_M_is_engaged())
+	  return std::move(this->_M_get());
+	__throw_bad_optional_access("Attempt to access value of a "
+				    "disengaged optional object");
       }
 
       constexpr const _Tp&&
       value() const&&
       {
-	return this->_M_is_engaged()
-	  ?  std::move(this->_M_get())
-	  : (__throw_bad_optional_access("Attempt to access value of a "
-		                         "disengaged optional object"),
-	     std::move(this->_M_get()));
+	if (this->_M_is_engaged())
+	  return std::move(this->_M_get());
+	__throw_bad_optional_access("Attempt to access value of a "
+				    "disengaged optional object");
       }
 
       template<typename _Up>
@@ -719,9 +711,10 @@ inline namespace fundamentals_v1
 			       is_convertible<_Up&&, _Tp>>(),
 			"Cannot return value");
 
-	  return this->_M_is_engaged()
-	    ? this->_M_get()
-	    : static_cast<_Tp>(std::forward<_Up>(__u));
+	  if (this->_M_is_engaged())
+	    return this->_M_get();
+	  else
+	    return static_cast<_Tp>(std::forward<_Up>(__u));
 	}
 
       template<typename _Up>
@@ -732,9 +725,10 @@ inline namespace fundamentals_v1
 			       is_convertible<_Up&&, _Tp>>(),
 			"Cannot return value" );
 
-	  return this->_M_is_engaged()
-	    ? std::move(this->_M_get())
-	    : static_cast<_Tp>(std::forward<_Up>(__u));
+	  if (this->_M_is_engaged())
+	    return std::move(this->_M_get());
+	  else
+	    return static_cast<_Tp>(std::forward<_Up>(__u));
 	}
     };
 
diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional
index 023fe81cae2..fbabb52758e 100644
--- a/libstdc++-v3/include/std/optional
+++ b/libstdc++-v3/include/std/optional
@@ -91,12 +91,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     { return "bad optional access"; }
   };
 
-  void
-  __throw_bad_optional_access()
-  __attribute__((__noreturn__));
-
   // XXX Does not belong here.
-  inline void
+  [[__noreturn__]] inline void
   __throw_bad_optional_access()
   { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
 
@@ -943,33 +939,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       constexpr const _Tp&
       value() const&
       {
-	return this->_M_is_engaged()
-	  ? this->_M_get()
-	  : (__throw_bad_optional_access(), this->_M_get());
+	if (this->_M_is_engaged())
+	  return this->_M_get();
+	__throw_bad_optional_access();
       }
 
       constexpr _Tp&
       value()&
       {
-	return this->_M_is_engaged()
-	  ? this->_M_get()
-	  : (__throw_bad_optional_access(), this->_M_get());
+	if (this->_M_is_engaged())
+	  return this->_M_get();
+	__throw_bad_optional_access();
       }
 
       constexpr _Tp&&
       value()&&
       {
-	return this->_M_is_engaged()
-	  ? std::move(this->_M_get())
-	  : (__throw_bad_optional_access(), std::move(this->_M_get()));
+	if (this->_M_is_engaged())
+	  return std::move(this->_M_get());
+	__throw_bad_optional_access();
       }
 
       constexpr const _Tp&&
       value() const&&
       {
-	return this->_M_is_engaged()
-	  ? std::move(this->_M_get())
-	  : (__throw_bad_optional_access(), std::move(this->_M_get()));
+	if (this->_M_is_engaged())
+	  return std::move(this->_M_get());
+	__throw_bad_optional_access();
       }
 
       template<typename _Up>
@@ -979,8 +975,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  static_assert(is_copy_constructible_v<_Tp>);
 	  static_assert(is_convertible_v<_Up&&, _Tp>);
 
-	  return this->_M_is_engaged()
-	    ? this->_M_get() : static_cast<_Tp>(std::forward<_Up>(__u));
+	  if (this->_M_is_engaged())
+	    return this->_M_get();
+	  else
+	    return static_cast<_Tp>(std::forward<_Up>(__u));
 	}
 
       template<typename _Up>
@@ -990,9 +988,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  static_assert(is_move_constructible_v<_Tp>);
 	  static_assert(is_convertible_v<_Up&&, _Tp>);
 
-	  return this->_M_is_engaged()
-	    ? std::move(this->_M_get())
-	    : static_cast<_Tp>(std::forward<_Up>(__u));
+	  if (this->_M_is_engaged())
+	    return std::move(this->_M_get());
+	  else
+	    return static_cast<_Tp>(std::forward<_Up>(__u));
 	}
 
       void reset() noexcept { this->_M_reset(); }


                 reply	other threads:[~2021-11-24 11:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211124115159.392D5385780D@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).