builtin-bit-cast.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s
  2. // RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s -fno-signed-char
  3. #if !__has_builtin(__builtin_bit_cast)
  4. #error
  5. #endif
  6. template <class T, T v>
  7. T instantiate() {
  8. return __builtin_bit_cast(T, v);
  9. }
  10. int x = instantiate<int, 32>();
  11. struct secret_ctor {
  12. char member;
  13. private: secret_ctor() = default;
  14. };
  15. void test1() {
  16. secret_ctor c = __builtin_bit_cast(secret_ctor, (char)0);
  17. }
  18. void test2() {
  19. constexpr int i = 0;
  20. // expected-error@+1{{__builtin_bit_cast source size does not equal destination size (4 vs 1)}}
  21. constexpr char c = __builtin_bit_cast(char, i);
  22. }
  23. struct not_trivially_copyable {
  24. virtual void foo() {}
  25. };
  26. // expected-error@+1{{__builtin_bit_cast source type must be trivially copyable}}
  27. constexpr unsigned long ul = __builtin_bit_cast(unsigned long, not_trivially_copyable{});
  28. // expected-error@+1 {{__builtin_bit_cast destination type must be trivially copyable}}
  29. constexpr long us = __builtin_bit_cast(unsigned long &, 0L);