From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id A0C753858016; Mon, 4 Apr 2022 11:47:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A0C753858016 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 r11-9773] libstdc++: Add missing constraints to std::bit_cast [PR105027] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 6f8133689f439716455d17d333c238d85add23ed X-Git-Newrev: 20f2303b0ce6bbe412e852e6423daabd04c39ebb Message-Id: <20220404114741.A0C753858016@sourceware.org> Date: Mon, 4 Apr 2022 11:47:41 +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: Mon, 04 Apr 2022 11:47:41 -0000 https://gcc.gnu.org/g:20f2303b0ce6bbe412e852e6423daabd04c39ebb commit r11-9773-g20f2303b0ce6bbe412e852e6423daabd04c39ebb Author: Jonathan Wakely 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. (cherry picked from commit 4894d69a1f37d54b6a612e58053db477ff5ba832) 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 c5aae8bab03..467ddbf4c0c 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 + +template +concept BitCastable = requires(const U& u) { std::bit_cast(u); }; + +static_assert(BitCastable); // OK + +static_assert(!BitCastable); // #1: different size + +struct A { A(A const&); int i; }; +static_assert(!BitCastable); // #2: not trivially copyable + +static_assert(!BitCastable); // #3: sizeof(int()) is ill-formed