xref: /llvm-project/clang/test/CXX/expr/expr.const/p5-26.cpp (revision caa902613a96f63c3855b3a0bcd82d1b1db49408)
1 // RUN: %clang_cc1 -fsyntax-only -std=c++2c -verify=expected,cxx26 %s
2 // RUN: %clang_cc1 -fsyntax-only -std=c++2b -verify=expected,cxx23 %s
3 
4 
5 struct S {};
6 struct T : S {} t;
7 
test()8 consteval void test() {
9     void* a = &t;
10     const void* b = &t;
11     volatile void* c = &t;
12     (void)static_cast<T*>(a);
13     (void)static_cast<const T*>(a);
14     (void)static_cast<volatile T*>(a);
15 
16     (void)(T*)(a);
17     (void)(const T*)(a);
18     (void)(volatile T*)(a);
19 
20     (void)static_cast<T*>(b); // expected-error {{static_cast from 'const void *' to 'T *' casts away qualifiers}}
21     (void)static_cast<volatile T*>(b); // expected-error {{static_cast from 'const void *' to 'volatile T *' casts away qualifiers}}
22     (void)static_cast<const T*>(b);
23     (void)static_cast<volatile const T*>(b);
24 
25     (void)static_cast<T*>(c); // expected-error{{static_cast from 'volatile void *' to 'T *' casts away qualifiers}}
26     (void)static_cast<volatile T*>(c);
27     (void)static_cast<const T*>(b);
28     (void)static_cast<volatile const T*>(b);
29 }
30 
err()31 void err() {
32     constexpr void* a = &t;
33     constexpr auto err1 = static_cast<int*>(a); // expected-error{{constexpr variable 'err1' must be initialized by a constant expression}} \
34                                                 // cxx23-note {{cast from 'void *' is not allowed in a constant expression in C++ standards before C++2c}} \
35                                                 // cxx26-note {{cast from 'void *' is not allowed in a constant expression because the pointed object type 'T' is not similar to the target type 'int'}}
36     constexpr auto err2 = static_cast<S*>(a);   // expected-error{{constexpr variable 'err2' must be initialized by a constant expression}} \
37                                                 // cxx23-note {{cast from 'void *' is not allowed in a constant expression in C++ standards before C++2c}} \
38                                                 // cxx26-note {{cast from 'void *' is not allowed in a constant expression because the pointed object type 'T' is not similar to the target type 'S'}}
39 }
40 
41 int* p;
42 constexpr int** pp = &p;
43 constexpr void* vp = pp;
44 constexpr auto cvp = static_cast<const int* volatile*>(vp);
45 // cxx23-error@-1 {{constant expression}}
46 // cxx23-note@-2 {{cast from 'void *' is not allowed in a constant expression}}
47