[libcxx-commits] [libcxx] [libc++] Add a test case for std::bit_cast with std::complex (PR #97751)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jul 6 10:38:11 PDT 2024


https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/97751

>From 5f57013b30fc3ca53d0ed3ade54fea6b710f383d Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 4 Jul 2024 12:04:14 -0400
Subject: [PATCH 1/2] [libc++] Add a test case for std::bit_cast with
 std::complex

This is extracted from #94620. While libc++ doesn't have the problem
described in that issue, a test case is a good idea to ensure that
we don't regress this behavior in the future. This could happen for
example if we decide to use `_Complex` in the implementation of
`std::complex` while Clang doesn't handle bit_cast with _Complex yet.
---
 .../complex.number/complex/bit_cast.pass.cpp  | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp

diff --git a/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp b/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp
new file mode 100644
index 0000000000000..7f15b6f9ade64
--- /dev/null
+++ b/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// Make sure that std::bit_cast works with std::complex. Test case extracted from
+// https://github.com/llvm/llvm-project/issues/94620.
+
+#include <bit>
+#include <complex>
+
+template <class T>
+constexpr void test() {
+  using Complex                       = std::complex<T>;
+  unsigned char data[sizeof(Complex)] = {0};
+
+  Complex c = std::bit_cast<Complex>(data);
+  (void)c;
+}
+
+constexpr bool test_all() {
+  test<float>();
+  test<double>();
+  test<long double>();
+  return true;
+}
+
+int main(int, char**) {
+  test_all();
+  static_assert(test_all());
+  return 0;
+}

>From 4a1c440172e1b1600e1d587fbecb9f79f2d328ab Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 4 Jul 2024 12:17:51 -0400
Subject: [PATCH 2/2] maybe-unused

---
 .../test/std/numerics/complex.number/complex/bit_cast.pass.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp b/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp
index 7f15b6f9ade64..c35d4d6c63295 100644
--- a/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp
+++ b/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp
@@ -19,8 +19,7 @@ constexpr void test() {
   using Complex                       = std::complex<T>;
   unsigned char data[sizeof(Complex)] = {0};
 
-  Complex c = std::bit_cast<Complex>(data);
-  (void)c;
+  [[maybe_unused]] Complex c = std::bit_cast<Complex>(data);
 }
 
 constexpr bool test_all() {



More information about the libcxx-commits mailing list