1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
7 // See if aliasing can confuse this baby.
13 typedef const cppp
&cpppcr
;
14 typedef const char cc
;
16 typedef volatile ccp ccvp
;
18 typedef const volatile ccvpp ccvpcvp
;
19 typedef ccvpcvp
*ccvpcvpp
;
22 typedef int (*f
)(int);
24 char ***good_const_cast_test(ccvpcvpp var
)
26 // Cast away deep consts and volatiles.
27 char ***var2
= const_cast<cppp
>(var
);
28 char ***const &var3
= var2
;
29 // Const reference to reference.
30 char ***&var4
= const_cast<cpppr
>(var3
);
31 // Drop reference. Intentionally without qualifier change.
32 char *** var5
= const_cast<cppp
>(var4
);
33 // Const array to array reference.
34 const int ar
[100] = {0};
35 int (&rar
)[100] = const_cast<iarr
>(ar
);
36 // Array decay. Intentionally without qualifier change.
37 int *pi
= const_cast<int*>(ar
);
39 // Don't misidentify fn** as a function pointer.
40 f
*fpp
= const_cast<f
*>(&fp
);
41 int const A::* const A::*icapcap
= 0;
42 int A::* A::* iapap
= const_cast<int A::* A::*>(icapcap
);
43 (void)const_cast<A
&&>(A());
44 #if __cplusplus <= 199711L // C++03 or earlier modes
45 // expected-warning@-2 {{rvalue references are a C++11 extension}}
50 short *bad_const_cast_test(char const *volatile *const volatile *var
)
52 // Different pointer levels.
53 char **var2
= const_cast<char**>(var
); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'char **' is not allowed}}
54 // Different final type.
55 short ***var3
= const_cast<short***>(var
); // expected-error {{const_cast from 'const char *volatile *const volatile *' to 'short ***' is not allowed}}
56 // Rvalue to reference.
57 char ***&var4
= const_cast<cpppr
>(&var2
); // expected-error {{const_cast from rvalue to reference type 'cpppr'}}
59 char v
= const_cast<char>(**var2
); // expected-error {{const_cast to 'char', which is not a reference, pointer-to-object, or pointer-to-data-member}}
60 const int *ar
[100] = {0};
61 extern const int *aub
[];
62 // const_cast looks through arrays as of DR330.
63 (void) const_cast<int *(*)[100]>(&ar
); // ok
64 (void) const_cast<int *(*)[]>(&aub
); // ok
65 // ... but the array bound must exactly match.
66 (void) const_cast<int *(*)[]>(&ar
); // expected-error {{const_cast from 'const int *(*)[100]' to 'int *(*)[]' is not allowed}}
67 (void) const_cast<int *(*)[99]>(&ar
); // expected-error {{const_cast from 'const int *(*)[100]' to 'int *(*)[99]' is not allowed}}
68 (void) const_cast<int *(*)[100]>(&aub
); // expected-error {{const_cast from 'const int *(*)[]' to 'int *(*)[100]' is not allowed}}
71 f fp2
= const_cast<f
>(fp1
); // expected-error {{const_cast to 'f' (aka 'int (*)(int)'), which is not a reference, pointer-to-object, or pointer-to-data-member}}
73 (void)const_cast<void (A::*)()>(mfn
); // expected-error-re {{const_cast to 'void (A::*)(){{( __attribute__\(\(thiscall\)\))?}}', which is not a reference, pointer-to-object, or pointer-to-data-member}}
74 (void)const_cast<int&&>(0); // expected-error {{const_cast from rvalue to reference type 'int &&'}}
75 #if __cplusplus <= 199711L // C++03 or earlier modes
76 // expected-warning@-2 {{rvalue references are a C++11 extension}}
82 char *PR21845() { return const_cast<char *>((void)T::x
); } // expected-error {{const_cast from 'void' to 'char *' is not allowed}}