| 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 |
|
| 4 |
#if !__has_builtin(__builtin_bit_cast) |
| 5 |
#error |
| 6 |
#endif |
| 7 |
|
| 8 |
template <class T, T v> |
| 9 |
T instantiate() { |
| 10 |
return __builtin_bit_cast(T, v); |
| 11 |
} |
| 12 |
|
| 13 |
int x = instantiate<int, 32>(); |
| 14 |
|
| 15 |
struct secret_ctor { |
| 16 |
char member; |
| 17 |
|
| 18 |
private: secret_ctor() = default; |
| 19 |
}; |
| 20 |
|
| 21 |
void test1() { |
| 22 |
secret_ctor c = __builtin_bit_cast(secret_ctor, (char)0); |
| 23 |
} |
| 24 |
|
| 25 |
void test2() { |
| 26 |
constexpr int i = 0; |
| 27 |
// expected-error@+1{{size of '__builtin_bit_cast' source type 'const int' does not match destination type 'char' (4 vs 1 bytes)}} |
| 28 |
constexpr char c = __builtin_bit_cast(char, i); |
| 29 |
} |
| 30 |
|
| 31 |
struct not_trivially_copyable { |
| 32 |
virtual void foo() {} |
| 33 |
}; |
| 34 |
|
| 35 |
// expected-error@+1{{'__builtin_bit_cast' source type must be trivially copyable}} |
| 36 |
constexpr unsigned long ul = __builtin_bit_cast(unsigned long, not_trivially_copyable{}); |
| 37 |
|
| 38 |
// expected-error@+1 {{'__builtin_bit_cast' destination type must be trivially copyable}} |
| 39 |
constexpr long us = __builtin_bit_cast(unsigned long &, 0L); |
| 40 |
|
| 41 |
namespace PR42936 { |
| 42 |
template <class T> struct S { int m; }; |
| 43 |
|
| 44 |
extern S<int> extern_decl; |
| 45 |
|
| 46 |
int x = __builtin_bit_cast(int, extern_decl); |
| 47 |
S<char> y = __builtin_bit_cast(S<char>, 0); |
| 48 |
} |