| 1 |
// { dg-do compile { target c++17 } } |
| 2 |
// { dg-options "-Wdangling-reference" } |
| 3 |
// Test -Wdangling-reference with std::minmax. |
| 4 |
|
| 5 |
#include <algorithm> |
| 6 |
|
| 7 |
using U = std::pair<const int&, const int&>; |
| 8 |
|
| 9 |
int |
| 10 |
fn1 () |
| 11 |
{ |
| 12 |
std::pair<const int&, const int&> v = std::minmax(1, 2); // { dg-warning "dangling reference" } |
| 13 |
U v2 = std::minmax(1, 2); // { dg-warning "dangling reference" } |
| 14 |
auto v3 = std::minmax(1, 2); // { dg-warning "dangling reference" } |
| 15 |
return v.first + v2.second + v3.first; |
| 16 |
} |
| 17 |
|
| 18 |
int |
| 19 |
fn2 () |
| 20 |
{ |
| 21 |
int n = 1; |
| 22 |
auto p = std::minmax(n, n + 1); // { dg-warning "dangling reference" } |
| 23 |
int m = p.first; // ok |
| 24 |
int x = p.second; // undefined behavior |
| 25 |
|
| 26 |
// Note that structured bindings have the same issue |
| 27 |
auto [mm, xx] = std::minmax(n, n + 1); // { dg-warning "dangling reference" } |
| 28 |
(void) xx; // undefined behavior |
| 29 |
|
| 30 |
return m + x; |
| 31 |
} |
| 32 |
|
| 33 |
int |
| 34 |
fn3 () |
| 35 |
{ |
| 36 |
auto v = std::minmax({1, 2}); |
| 37 |
return v.first; |
| 38 |
} |