Debian Codegraph @

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

raw
1 // PR c++/101165 - P2266R1 - Simpler implicit move
2 // { dg-do compile { target c++23 } }
3 // { dg-options "-Wdangling-reference" }
4 // Test from P2266R1, $ 5.2. LibreOffice OString constructor.
5
6 struct X {
7 X(auto&);
8 };
9
10 // The following compiles in C++20 (deducing X(char (&)[10])) but not
11 // after P2266 (because the returned expression now has type char (&&)[10],
12 // which cannot bind to auto&).
13 X f() {
14 char a[10];
15 return a; // { dg-error "cannot bind non-const lvalue reference" }
16 }
17
18 // The solution was to change it by making the return convert explicitly
19 // rather than implicitly:
20 X fixed() {
21 char a[10];
22 return X(a);
23 }
24
25 // $ 5.3. LibreOffice o3tl::temporary
26
27 template<class T>
28 T& temporary1(T&& x) { return x; } // { dg-error "cannot bind non-const lvalue reference" }
29
30 // Fixed by:
31 template<class T>
32 T& temporary2(T&& x) { return static_cast<T&>(x); }
33
34 void
35 test ()
36 {
37 int& r1 = temporary1 (42);
38 int& r2 = temporary2 (42);
39 }