From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 652F13857423; Thu, 16 Sep 2021 22:05:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 652F13857423 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-3589] libstdc++: Add noexcept to std::to_string overloads that don't allocate X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 869107c9c9752c9a53cdb06179c1e6be6d2e5f44 X-Git-Newrev: 9d813ddd978aff75001d53fe55ff15e9167bb4d0 Message-Id: <20210916220529.652F13857423@sourceware.org> Date: Thu, 16 Sep 2021 22:05:29 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Sep 2021 22:05:29 -0000 https://gcc.gnu.org/g:9d813ddd978aff75001d53fe55ff15e9167bb4d0 commit r12-3589-g9d813ddd978aff75001d53fe55ff15e9167bb4d0 Author: Jonathan Wakely Date: Wed Sep 15 21:40:20 2021 +0100 libstdc++: Add noexcept to std::to_string overloads that don't allocate When the values is guaranteed to fit in the SSO buffer we know the string won't allocate, so the function can be noexcept. For 32-bit integers, we know they need no more than 9 bytes (or 10 with a minus sign) and the SSO buffer is 15 bytes. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/basic_string.h [_GLIBCXX_USE_CXX11_ABI] (to_string): Add noexcept if the type width is 32 bits or less. Diff: --- libstdc++-v3/include/bits/basic_string.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index b61fe05efcf..24c454d863a 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -3718,6 +3718,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline string to_string(int __val) +#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32 + noexcept // any 32-bit value fits in the SSO buffer +#endif { const bool __neg = __val < 0; const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val; @@ -3729,6 +3732,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline string to_string(unsigned __val) +#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32 + noexcept // any 32-bit value fits in the SSO buffer +#endif { string __str(__detail::__to_chars_len(__val), '\0'); __detail::__to_chars_10_impl(&__str[0], __str.size(), __val); @@ -3737,6 +3743,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline string to_string(long __val) +#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32 + noexcept // any 32-bit value fits in the SSO buffer +#endif { const bool __neg = __val < 0; const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val; @@ -3748,6 +3757,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline string to_string(unsigned long __val) +#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32 + noexcept // any 32-bit value fits in the SSO buffer +#endif { string __str(__detail::__to_chars_len(__val), '\0'); __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);