Clang Project

clang_source_code/test/SemaCXX/rval-references.cpp
1// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only  -verify -std=c++11 %s
2
3typedef int&& irr;
4typedef irr& ilr_c1; // Collapses to int&
5typedef int& ilr;
6typedef ilr&& ilr_c2; // Collapses to int&
7
8irr ret_irr() {
9  return 0; // expected-warning {{returning reference to local temporary}}
10}
11
12struct not_int {};
13
14int over(int&);
15not_int over(int&&);
16
17int over2(const int&);
18not_int over2(int&&);
19
20struct conv_to_not_int_rvalue {
21  operator not_int &&();
22};
23
24typedef void (fun_type)();
25void fun();
26fun_type &&make_fun();
27
28void f() {
29  int &&virr1; // expected-error {{declaration of reference variable 'virr1' requires an initializer}}
30  int &&virr2 = 0;
31  int &&virr3 = virr2; // expected-error {{rvalue reference to type 'int' cannot bind to lvalue of type 'int'}}
32  int i1 = 0;
33  const double d1 = 0;
34  const int ci1 = 1;
35  int &&virr4 = i1; // expected-error {{rvalue reference to type 'int' cannot bind to lvalue of type 'int'}}
36  int &&virr5 = ret_irr();
37  int &&virr6 = static_cast<int&&>(i1);
38  (void)static_cast<not_int &&>(i1); // expected-error {{reference to type 'not_int' could not bind to an lvalue of type 'int'}}
39  (void)static_cast<int &&>(static_cast<int const&&>(i1)); // expected-error {{cannot cast from rvalue of type 'const int' to rvalue reference type 'int &&'}}
40  (void)static_cast<int &&>(ci1);    // expected-error {{types are not compatible}}
41  (void)static_cast<int &&>(d1);
42  int i2 = over(i1);
43  not_int ni1 = over(0);
44  int i3 = over(virr2);
45  not_int ni2 = over(ret_irr());
46
47  int i4 = over2(i1);
48  not_int ni3 = over2(0);
49
50  ilr_c1 vilr1 = i1;
51  ilr_c2 vilr2 = i1;
52
53  conv_to_not_int_rvalue cnir;
54  not_int &&ni4 = cnir;
55  not_int &ni5 = cnir; // expected-error{{non-const lvalue reference to type 'not_int' cannot bind to a value of unrelated type 'conv_to_not_int_rvalue'}}
56  not_int &&ni6 = conv_to_not_int_rvalue();
57
58  fun_type &&fun_ref = fun; // works because functions are special
59  fun_type &&fun_ref2 = make_fun(); // same
60  fun_type &fun_lref = make_fun(); // also special
61
62  try {
63  } catch(int&&) { // expected-error {{cannot catch exceptions by rvalue reference}}
64  }
65}
66
67int&& should_warn(int i) {
68  return static_cast<int&&>(i); // expected-warning {{reference to stack memory associated with parameter 'i' returned}}
69}
70int&& should_not_warn(int&& i) {
71  return static_cast<int&&>(i);
72}
73
74
75// Test the return dance. This also tests IsReturnCopyElidable.
76struct MoveOnly {
77  MoveOnly();
78  MoveOnly(const MoveOnly&) = delete; // expected-note 3{{explicitly marked deleted here}}
79};
80
81MoveOnly gmo;
82MoveOnly returningNonEligible() {
83  static MoveOnly mo;
84  MoveOnly &r = mo;
85  if (0) // Copy from global can't be elided
86    return gmo; // expected-error {{call to deleted constructor}}
87  else if (0) // Copy from local static can't be elided
88    return mo; // expected-error {{call to deleted constructor}}
89  else // Copy from reference can't be elided
90    return r; // expected-error {{call to deleted constructor}}
91}
92