xref: /llvm-project/clang/test/CXX/expr/expr.post/expr.const.cast/p1-0x.cpp (revision 82c9b5183f97930e8fb146d6b2eece6cb4d24652)
19ca5c425SRichard Smith // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2c1ed20cfSDouglas Gregor 
3c1ed20cfSDouglas Gregor // The result of the expression const_cast<T>(v) is of type T. If T is
4c1ed20cfSDouglas Gregor // an lvalue reference to object type, the result is an lvalue; if T
5c1ed20cfSDouglas Gregor // is an rvalue reference to object type, the result is an xvalue;.
6c1ed20cfSDouglas Gregor 
7c1ed20cfSDouglas Gregor unsigned int f(int);
8c1ed20cfSDouglas Gregor 
9*82c9b518SRichard Smith struct X {};
10*82c9b518SRichard Smith 
11c1ed20cfSDouglas Gregor template<typename T> T& lvalue();
12c1ed20cfSDouglas Gregor template<typename T> T&& xvalue();
13c1ed20cfSDouglas Gregor template<typename T> T prvalue();
14c1ed20cfSDouglas Gregor 
test_classification(const int * ptr,X x)15*82c9b518SRichard Smith void test_classification(const int *ptr, X x) {
16*82c9b518SRichard Smith   int *&&ptr0 = const_cast<int *&&>(ptr);
17*82c9b518SRichard Smith   int *&&ptr1 = const_cast<int *&&>(xvalue<const int*>());
18*82c9b518SRichard Smith   int *&&ptr2 = const_cast<int *&&>(prvalue<const int*>()); // expected-error {{const_cast from rvalue to reference type 'int *&&'}}
19*82c9b518SRichard Smith   X &&ptr3 = const_cast<X&&>(x);
20*82c9b518SRichard Smith   X &&ptr4 = const_cast<X&&>(xvalue<X>());
21*82c9b518SRichard Smith   X &&ptr5 = const_cast<X&&>(prvalue<X>());
22*82c9b518SRichard Smith 
23*82c9b518SRichard Smith   int *&ptr6 = const_cast<int *&>(ptr);
24*82c9b518SRichard Smith   int *&ptr7 = const_cast<int *&>(xvalue<const int*>()); // expected-error {{const_cast from rvalue to reference type 'int *&'}}
25*82c9b518SRichard Smith   int *&ptr8 = const_cast<int *&>(prvalue<const int*>()); // expected-error {{const_cast from rvalue to reference type 'int *&'}}
26*82c9b518SRichard Smith   X &ptr9 = const_cast<X&>(x);
27*82c9b518SRichard Smith   X &ptrA = const_cast<X&>(xvalue<X>()); // expected-error {{const_cast from rvalue to reference type 'X &'}}
28*82c9b518SRichard Smith   X &ptrB = const_cast<X&>(prvalue<X>()); // expected-error {{const_cast from rvalue to reference type 'X &'}}
29c1ed20cfSDouglas Gregor }
30d25db7edSJohn McCall 
31d25db7edSJohn McCall struct A {
32d25db7edSJohn McCall   volatile unsigned ubf : 4;
33d25db7edSJohn McCall   volatile unsigned uv;
34d25db7edSJohn McCall   volatile int sv;
35d25db7edSJohn McCall   void foo();
36d25db7edSJohn McCall   bool pred();
37d25db7edSJohn McCall };
38d25db7edSJohn McCall 
test(A & a)39d25db7edSJohn McCall void test(A &a) {
40d25db7edSJohn McCall   unsigned &t0 = const_cast<unsigned&>(a.ubf); // expected-error {{const_cast from bit-field lvalue to reference type}}
41d25db7edSJohn McCall   unsigned &t1 = const_cast<unsigned&>(a.foo(), a.ubf); // expected-error {{const_cast from bit-field lvalue to reference type}}
42d25db7edSJohn McCall   unsigned &t2 = const_cast<unsigned&>(a.pred() ? a.ubf : a.ubf); // expected-error {{const_cast from bit-field lvalue to reference type}}
43d25db7edSJohn McCall   unsigned &t3 = const_cast<unsigned&>(a.pred() ? a.ubf : a.uv); // expected-error {{const_cast from bit-field lvalue to reference type}}
44d25db7edSJohn McCall   unsigned &t4 = const_cast<unsigned&>(a.pred() ? a.ubf : a.sv); // expected-error {{const_cast from rvalue to reference type}}
45d25db7edSJohn McCall }
46