1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s 2575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor 3575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor// The result of the expression const_cast<T>(v) is of type T. If T is 4575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor// an lvalue reference to object type, the result is an lvalue; if T 5575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor// is an rvalue reference to object type, the result is an xvalue;. 6575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor 7575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregorunsigned int f(int); 8575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor 941cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smithstruct X {}; 1041cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith 11575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregortemplate<typename T> T& lvalue(); 12575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregortemplate<typename T> T&& xvalue(); 13575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregortemplate<typename T> T prvalue(); 14575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor 1541cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smithvoid test_classification(const int *ptr, X x) { 1641cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith int *&&ptr0 = const_cast<int *&&>(ptr); 1741cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith int *&&ptr1 = const_cast<int *&&>(xvalue<const int*>()); 1841cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith int *&&ptr2 = const_cast<int *&&>(prvalue<const int*>()); // expected-error {{const_cast from rvalue to reference type 'int *&&'}} 1941cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith X &&ptr3 = const_cast<X&&>(x); 2041cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith X &&ptr4 = const_cast<X&&>(xvalue<X>()); 2141cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith X &&ptr5 = const_cast<X&&>(prvalue<X>()); 2241cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith 2341cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith int *&ptr6 = const_cast<int *&>(ptr); 2441cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith int *&ptr7 = const_cast<int *&>(xvalue<const int*>()); // expected-error {{const_cast from rvalue to reference type 'int *&'}} 2541cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith int *&ptr8 = const_cast<int *&>(prvalue<const int*>()); // expected-error {{const_cast from rvalue to reference type 'int *&'}} 2641cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith X &ptr9 = const_cast<X&>(x); 2741cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith X &ptrA = const_cast<X&>(xvalue<X>()); // expected-error {{const_cast from rvalue to reference type 'X &'}} 2841cb3d90c2114a7df7aa04f80c8be4b62994fb0dRichard Smith X &ptrB = const_cast<X&>(prvalue<X>()); // expected-error {{const_cast from rvalue to reference type 'X &'}} 29575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor} 30993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall 31993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCallstruct A { 32993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall volatile unsigned ubf : 4; 33993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall volatile unsigned uv; 34993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall volatile int sv; 35993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall void foo(); 36993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall bool pred(); 37993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall}; 38993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall 39993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCallvoid test(A &a) { 40993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall unsigned &t0 = const_cast<unsigned&>(a.ubf); // expected-error {{const_cast from bit-field lvalue to reference type}} 41993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall unsigned &t1 = const_cast<unsigned&>(a.foo(), a.ubf); // expected-error {{const_cast from bit-field lvalue to reference type}} 42993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall unsigned &t2 = const_cast<unsigned&>(a.pred() ? a.ubf : a.ubf); // expected-error {{const_cast from bit-field lvalue to reference type}} 43993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall unsigned &t3 = const_cast<unsigned&>(a.pred() ? a.ubf : a.uv); // expected-error {{const_cast from bit-field lvalue to reference type}} 44993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall unsigned &t4 = const_cast<unsigned&>(a.pred() ? a.ubf : a.sv); // expected-error {{const_cast from rvalue to reference type}} 45993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall} 46