public inbox for gcc-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 r12-7781] libstdc++: Add missing constraints to std::bit_cast [PR105027] Date: Wed, 23 Mar 2022 12:17:35 +0000 (GMT) [thread overview] Message-ID: <20220323121735.472513858405@sourceware.org> (raw) https://gcc.gnu.org/g:4894d69a1f37d54b6a612e58053db477ff5ba832 commit r12-7781-g4894d69a1f37d54b6a612e58053db477ff5ba832 Author: Jonathan Wakely <jwakely@redhat.com> Date: Wed Mar 23 09:57:20 2022 +0000 libstdc++: Add missing constraints to std::bit_cast [PR105027] Our std::bit_cast was relying on the compiler to check for errors inside __builtin_bit_cast, instead of checking them as constraints. That means std::bit_cast was not SFINAE-friendly. This fix uses a requires-clause, so for old versions of Clang without concepts support the function will still be unconstrained. At some point in future we can remove the #ifdef __cpp_concepts check and rely on all compilers having full concepts support in C++20 mode. libstdc++-v3/ChangeLog: PR libstdc++/105027 * include/std/bit (bit_cast): Add constraints. * testsuite/26_numerics/bit/bit.cast/105027.cc: New test. Diff: --- libstdc++-v3/include/std/bit | 4 ++++ .../testsuite/26_numerics/bit/bit.cast/105027.cc | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit index cfd98e24eb5..a40f1ce99df 100644 --- a/libstdc++-v3/include/std/bit +++ b/libstdc++-v3/include/std/bit @@ -73,6 +73,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION [[nodiscard]] constexpr _To bit_cast(const _From& __from) noexcept +#ifdef __cpp_concepts + requires (sizeof(_To) == sizeof(_From)) + && __is_trivially_copyable(_To) && __is_trivially_copyable(_From) +#endif { return __builtin_bit_cast(_To, __from); } diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bit.cast/105027.cc b/libstdc++-v3/testsuite/26_numerics/bit/bit.cast/105027.cc new file mode 100644 index 00000000000..301d94ec575 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/bit/bit.cast/105027.cc @@ -0,0 +1,18 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } + +// PR libstdc++/105027 - Missing constraints on std::bit_cast + +#include <bit> + +template<class T, class U> +concept BitCastable = requires(const U& u) { std::bit_cast<T>(u); }; + +static_assert(BitCastable<int, unsigned>); // OK + +static_assert(!BitCastable<int, char>); // #1: different size + +struct A { A(A const&); int i; }; +static_assert(!BitCastable<int, A>); // #2: not trivially copyable + +static_assert(!BitCastable<long, int()>); // #3: sizeof(int()) is ill-formed
reply other threads:[~2022-03-23 12:17 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=20220323121735.472513858405@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: linkBe 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).