From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id AA960398383F for ; Tue, 27 Jul 2021 20:39:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AA960398383F Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-388-VsH7CtdvOx-WOS4UvkgclA-1; Tue, 27 Jul 2021 16:38:59 -0400 X-MC-Unique: VsH7CtdvOx-WOS4UvkgclA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B4C5E180FCD0; Tue, 27 Jul 2021 20:38:58 +0000 (UTC) Received: from localhost (ovpn-113-85.ams2.redhat.com [10.36.113.85]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57C9E5D9FC; Tue, 27 Jul 2021 20:38:58 +0000 (UTC) Date: Tue, 27 Jul 2021 21:38:57 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Simplify std::optional::value() Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="Eu5cc76/EWO/g82c" Content-Disposition: inline X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Jul 2021 20:39:03 -0000 --Eu5cc76/EWO/g82c Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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. Tested powerpc64le-linux. Committed to trunk. --Eu5cc76/EWO/g82c Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 9360d6cd1706882dfffd3c7a08b5956c37207a11 Author: Jonathan Wakely Date: Tue Jul 27 14:50:28 2021 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. diff --git a/libstdc++-v3/include/experimental/optional b/libstdc++-v3/include/experimental/optional index 431d23631cf..274c974853d 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 @@ -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 @@ -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 df9ed0736b3..b8ab7510757 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 @@ -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 @@ -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(); } --Eu5cc76/EWO/g82c--