[libcxx-commits] [PATCH] D75960: [libc++] Implement C++20's P0476r2: std::bit_cast

Zoe Carver via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Dec 9 12:49:49 PST 2020

zoecarver added a comment.

This approach may be frowned upon because `std::bit_cast` is implemented with `__builtin_bit_cast` but, I think you could actually use `__builtin_bit_cast` to write the comparison function you want for the tests:

  template<class T> struct Buff { char buff[sizeof(T)]; };
  template<class T>
  constexpr bool cmp(const T &a, const T &b) {
      auto c1 = __builtin_bit_cast(Buff<T>, a);
      auto c2 = __builtin_bit_cast(Buff<T>, b);
      for (unsigned i = 0; i < sizeof(T); ++i) {
          if (c1.buff[i] != c2.buff[i])
              return false;
      return true;

Comment at: libcxx/test/std/numerics/bit/bit.cast/bit_cast.fail.cpp:31
+int main(int, char**)
+    // Not the same size
Maybe a deleted copy constructor? 

Comment at: libcxx/test/std/numerics/bit/bit.cast/bit_cast.pass.cpp:43
+    }
+    assert(__builtin_memcmp(&value1, &value2, sizeof(T)) == 0);
So, in non-constexpr, the only thing you're testing here is that you can cast a pointer to its current type?

  rG LLVM Github Monorepo



More information about the libcxx-commits mailing list