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