| 1 |
// RUN: %clang_cc1 -std=c++26 -fsyntax-only -fcxx-exceptions -verify=ref,both %s |
| 2 |
// RUN: %clang_cc1 -std=c++26 -fsyntax-only -fcxx-exceptions -verify=expected,both %s -fexperimental-new-constant-interpreter |
| 3 |
|
| 4 |
namespace std { |
| 5 |
using size_t = decltype(sizeof(0)); |
| 6 |
} |
| 7 |
|
| 8 |
namespace VoidCast { |
| 9 |
constexpr void* p = nullptr; |
| 10 |
constexpr int* q = static_cast<int*>(p); |
| 11 |
static_assert(q == nullptr); |
| 12 |
|
| 13 |
static_assert((delete (int*)(void*)new int, true)); |
| 14 |
static_assert((delete[] (int*)(void*)new int[2], true)); |
| 15 |
|
| 16 |
static_assert((delete (float*)(void*)new int, true)); // both-error {{not an integral constant expression}} \ |
| 17 |
// both-note {{cast from 'void *' is not allowed in a constant expression because the pointed object type 'int' is not similar to the target type 'float'}} |
| 18 |
|
| 19 |
static_assert((delete[] (float*)(void*)new int[2], true)); // both-error {{not an integral constant expression}} \ |
| 20 |
// both-note {{cast from 'void *' is not allowed in a constant expression because the pointed object type 'int' is not similar to the target type 'float'}} |
| 21 |
} |
| 22 |
|
| 23 |
namespace ReplaceableAlloc { |
| 24 |
struct F { |
| 25 |
static void* operator new(std::size_t n) { |
| 26 |
return nullptr; // both-warning {{should not return a null pointer}} |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
constexpr F *createF() { |
| 31 |
return new F(); // both-note {{call to class-specific 'operator new'}} |
| 32 |
} |
| 33 |
|
| 34 |
constexpr bool foo() { |
| 35 |
F *f = createF(); // both-note {{in call to}} |
| 36 |
|
| 37 |
delete f; |
| 38 |
return true; |
| 39 |
} |
| 40 |
static_assert(foo()); // both-error {{not an integral constant expression}} \ |
| 41 |
// both-note {{in call to}} |
| 42 |
} |
| 43 |
|
| 44 |
constexpr int a = 12; |
| 45 |
constexpr const int *b = &a; |
| 46 |
constexpr int *f = (int*)(void*)b; |
| 47 |
static_assert(*f == 12); |