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