rval-references.cpp revision b1622a1fd7b7f4ab8d00d0183d17c90ad25c14e3
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x %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;
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
24void f() {
25  int &&virr1; // expected-error {{declaration of reference variable 'virr1' requires an initializer}}
26  int &&virr2 = 0;
27  int &&virr3 = virr2; // expected-error {{rvalue reference cannot bind to lvalue}}
28  int i1 = 0;
29  int &&virr4 = i1; // expected-error {{rvalue reference cannot bind to lvalue}}
30  int &&virr5 = ret_irr();
31  int &&virr6 = static_cast<int&&>(i1);
32  (void)static_cast<not_int&&>(i1); // expected-error {{types are not compatible}}
33
34  int i2 = over(i1);
35  not_int ni1 = over(0);
36  int i3 = over(virr2);
37  not_int ni2 = over(ret_irr());
38
39  int i4 = over2(i1);
40  not_int ni3 = over2(0);
41
42  ilr_c1 vilr1 = i1;
43  ilr_c2 vilr2 = i1;
44
45  conv_to_not_int_rvalue cnir;
46  not_int &&ni4 = cnir; // expected-error {{rvalue reference cannot bind to lvalue}}
47  not_int &ni5 = cnir; // expected-error{{non-const lvalue reference to type 'struct not_int' cannot bind to a value of unrelated type 'struct conv_to_not_int_rvalue'}}
48  not_int &&ni6 = conv_to_not_int_rvalue();
49
50
51  try {
52  } catch(int&&) { // expected-error {{cannot catch exceptions by rvalue reference}}
53  }
54}
55
56int&& should_warn(int i) {
57  // FIXME: The stack address return test doesn't reason about casts.
58  return static_cast<int&&>(i); // xpected-warning {{returning reference to temporary}}
59}
60int&& should_not_warn(int&& i) { // But GCC 4.4 does
61  return static_cast<int&&>(i);
62}
63
64
65// Test the return dance. This also tests IsReturnCopyElidable.
66struct MoveOnly {
67  MoveOnly();
68  MoveOnly(const MoveOnly&) = delete;	// expected-note {{candidate function}} \
69  // expected-note 3{{explicitly marked deleted here}}
70  MoveOnly(MoveOnly&&);	// expected-note {{candidate constructor}}
71  MoveOnly(int&&);	// expected-note {{candidate constructor}}
72};
73
74MoveOnly gmo;
75MoveOnly returningNonEligible() {
76  int i;
77  static MoveOnly mo;
78  MoveOnly &r = mo;
79  if (0) // Copy from global can't be elided
80    return gmo; // expected-error {{call to deleted constructor}}
81  else if (0) // Copy from local static can't be elided
82    return mo; // expected-error {{call to deleted constructor}}
83  else if (0) // Copy from reference can't be elided
84    return r; // expected-error {{call to deleted constructor}}
85  else // Construction from different type can't be elided
86    return i; // expected-error {{no viable conversion from 'int' to 'struct MoveOnly'}}
87}
88