p5-0x.cpp revision 564cb06b1f010ab3c5e316ac79ba6cfdd72e9c1d
1// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify -pedantic %s 2 3// Test the C++0x-specific reference initialization rules, e.g., the 4// rules for rvalue references. 5template<typename T> T prvalue(); 6template<typename T> T&& xvalue(); 7template<typename T> T& lvalue(); 8 9struct Base { }; 10struct Derived : Base { }; 11 12struct HasArray { 13 int array[5]; 14}; 15 16int f(int); 17 18template<typename T> 19struct ConvertsTo { 20 operator T(); 21}; 22 23void test_rvalue_refs() { 24 // If the initializer expression... 25 // - is an xvalue, class prvalue, array prvalue or function lvalue 26 // and "cv1 T1" is reference-compatible with "cv2 T2", or 27 28 // xvalue case 29 Base&& base0 = xvalue<Base>(); 30 Base&& base1 = xvalue<Derived>(); 31 int&& int0 = xvalue<int>(); 32 33 // class prvalue case 34 Base&& base2 = prvalue<Base>(); 35 Base&& base3 = prvalue<Derived>(); 36 37 // array prvalue case 38 int (&&array0)[5] = HasArray().array; 39 40 // function lvalue case 41 int (&&function0)(int) = f; 42 43 // - has a class type (i.e., T2 is a class type), where T1 is not 44 // reference-related to T2, and can be implicitly converted to 45 // an xvalue, class prvalue, or function lvalue of type "cv3 46 // T3", where "cv1 T1" is reference-compatible with "cv3 T3", 47 48 // xvalue 49 Base&& base4 = ConvertsTo<Base&&>(); 50 Base&& base5 = ConvertsTo<Derived&&>(); 51 int && int1 = ConvertsTo<int&&>(); 52 53 // class prvalue 54 Base&& base6 = ConvertsTo<Base>(); 55 Base&& base7 = ConvertsTo<Derived>(); 56 57 // function lvalue 58 int (&&function1)(int) = ConvertsTo<int(&)(int)>(); 59} 60 61class NonCopyable { 62 NonCopyable(const NonCopyable&); 63}; 64 65class NonCopyableDerived : public NonCopyable { 66 NonCopyableDerived(const NonCopyableDerived&); 67}; 68 69// Make sure we get direct bindings with no copies. 70void test_direct_binding() { 71 NonCopyable &&nc0 = prvalue<NonCopyable>(); 72 NonCopyable &&nc1 = prvalue<NonCopyableDerived>(); 73 NonCopyable &&nc2 = xvalue<NonCopyable>(); 74 NonCopyable &&nc3 = xvalue<NonCopyableDerived>(); 75 const NonCopyable &nc4 = prvalue<NonCopyable>(); 76 const NonCopyable &nc5 = prvalue<NonCopyableDerived>(); 77 const NonCopyable &nc6 = xvalue<NonCopyable>(); 78 const NonCopyable &nc7 = xvalue<NonCopyableDerived>(); 79 NonCopyable &&nc8 = ConvertsTo<NonCopyable&&>(); 80 NonCopyable &&nc9 = ConvertsTo<NonCopyableDerived&&>(); 81 const NonCopyable &nc10 = ConvertsTo<NonCopyable&&>(); 82 const NonCopyable &nc11 = ConvertsTo<NonCopyableDerived&&>(); 83} 84