aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp23/elision5.C
blob: a7d3e7c27c4a1e7ff4d9a689e1bb3ca4b4eecdc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// PR c++/101165 - P2266R1 - Simpler implicit move
// { dg-do compile { target c++23 } }
// Test from [class.copy.elision]/4.

class Thing {
public:
  Thing();
  ~Thing();
  Thing(Thing&&);
private:
  Thing(const Thing&);
};

Thing f(bool b) {
  Thing t;
  if (b)
    throw t;            // OK, Thing(Thing&&) used (or elided) to throw t
  return t;             // OK, Thing(Thing&&) used (or elided) to return t
}

Thing t2 = f(false);    // OK, no extra copy/move performed, t2 constructed by call to f

struct Weird {
  Weird();
  Weird(Weird&);
};

Weird g(bool b) {
  static Weird w1;
  Weird w2;
  if (b) {
    return w1;  // OK: Weird(Weird&)
  } else {
    return w2;  // { dg-error "cannot bind non-const lvalue reference" }
  }
}

int& h(bool b, int i) {
  static int s;
  if (b)
    return s;   // OK
  else
    return i;   // { dg-error "cannot bind non-const lvalue reference" }
}

decltype(auto) h2(Thing t) {
  return t;     // OK, t is an xvalue and h2's return type is Thing
}

decltype(auto) h3(Thing t) {
  // OK, (t) is an xvalue and h3's return type is Thing&&
  return (t); // { dg-warning "reference to local variable" }
}