Debian Codegraph @

gcc/testsuite/g++.dg/cpp23/elision5.C

raw
1 // PR c++/101165 - P2266R1 - Simpler implicit move
2 // { dg-do compile { target c++23 } }
3 // Test from [class.copy.elision]/4.
4
5 class Thing {
6 public:
7 Thing();
8 ~Thing();
9 Thing(Thing&&);
10 private:
11 Thing(const Thing&);
12 };
13
14 Thing f(bool b) {
15 Thing t;
16 if (b)
17 throw t; // OK, Thing(Thing&&) used (or elided) to throw t
18 return t; // OK, Thing(Thing&&) used (or elided) to return t
19 }
20
21 Thing t2 = f(false); // OK, no extra copy/move performed, t2 constructed by call to f
22
23 struct Weird {
24 Weird();
25 Weird(Weird&);
26 };
27
28 Weird g(bool b) {
29 static Weird w1;
30 Weird w2;
31 if (b) {
32 return w1; // OK: Weird(Weird&)
33 } else {
34 return w2; // { dg-error "cannot bind non-const lvalue reference" }
35 }
36 }
37
38 int& h(bool b, int i) {
39 static int s;
40 if (b)
41 return s; // OK
42 else
43 return i; // { dg-error "cannot bind non-const lvalue reference" }
44 }
45
46 decltype(auto) h2(Thing t) {
47 return t; // OK, t is an xvalue and h2's return type is Thing
48 }
49
50 decltype(auto) h3(Thing t) {
51 // OK, (t) is an xvalue and h3's return type is Thing&&
52 return (t); // { dg-warning "reference to local variable" }
53 }