From: Jonathan Wakely Date: Mon, 14 Jun 2021 21:42:05 +0000 (+0100) Subject: libstdc++: Fix noexcept-specifier for ranges::empty X-Git-Tag: releases/gcc-10.4.0~294 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=01c72ccad94e4f7b71504e502cba43d62b658143;p=thirdparty%2Fgcc.git libstdc++: Fix noexcept-specifier for ranges::empty Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/range_access.h (ranges::empty): Check whether conversion to bool can throw. * testsuite/std/ranges/access/empty.cc: Check for correct noexcept-specifier. (cherry picked from commit f9598d89a9f5a327ecdfa6f6978a0cfbe4447111) --- diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h index 36bc55e6559a..aee72c3fae40 100644 --- a/libstdc++-v3/include/bits/range_access.h +++ b/libstdc++-v3/include/bits/range_access.h @@ -773,7 +773,7 @@ namespace ranges _S_noexcept() { if constexpr (__member_empty<_Tp>) - return noexcept(std::declval<_Tp>().empty()); + return noexcept(bool(std::declval<_Tp>().empty())); else if constexpr (__size0_empty<_Tp>) return noexcept(_Size{}(std::declval<_Tp>()) == 0); else diff --git a/libstdc++-v3/testsuite/std/ranges/access/empty.cc b/libstdc++-v3/testsuite/std/ranges/access/empty.cc index 4b54ed7b3678..47dfca6513f9 100644 --- a/libstdc++-v3/testsuite/std/ranges/access/empty.cc +++ b/libstdc++-v3/testsuite/std/ranges/access/empty.cc @@ -68,9 +68,40 @@ test02() VERIFY( !std::ranges::empty(so) ); } +void +test04() +{ + struct E1 + { + bool empty() const noexcept { return {}; } + }; + + static_assert( noexcept(std::ranges::empty(E1{})) ); + + struct E2 + { + bool empty() const noexcept(false) { return {}; } + }; + + static_assert( ! noexcept(std::ranges::empty(E2{})) ); + + struct E3 + { + struct B + { + explicit operator bool() const noexcept(false) { return true; } + }; + + B empty() const noexcept { return {}; } + }; + + static_assert( ! noexcept(std::ranges::empty(E3{})) ); +} + int main() { test01(); test02(); + test04(); }