diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2021-07-27 14:50:28 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2021-07-27 21:36:01 +0100 |
commit | 9360d6cd1706882dfffd3c7a08b5956c37207a11 (patch) | |
tree | e03975117aa828b80f557d0d4f7695f8696c00f8 /libstdc++-v3/include/experimental | |
parent | b7195fb01fe62a313ae5f7faede698101bdb3025 (diff) |
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.
Diffstat (limited to 'libstdc++-v3/include/experimental')
-rw-r--r-- | libstdc++-v3/include/experimental/optional | 56 |
1 files changed, 25 insertions, 31 deletions
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<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)); } }; |