<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61567>61567</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Aggregate parens-init checks for dangling too aggressively
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
timsong-cpp
</td>
</tr>
</table>
<pre>
Clang rejects the well-formed but dangling cases from http://eel.is/c++draft/dcl.init.general#example-3:
```cpp
struct A {
int a;
int&& r;
};
int f();
int n = 10;
A a2(1, f()); // well-formed, but dangling reference
A a4(1.0, 1); // well-formed, but dangling reference
```
```
<source>:9:9: error: reference member 'r' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object
A a2(1, f()); // well-formed, but dangling reference
^~~
<source>:3:9: note: reference member declared here
int&& r;
^
<source>:10:11: error: reference member 'r' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object
A a4(1.0, 1); // well-formed, but dangling reference
^
<source>:3:9: note: reference member declared here
int&& r;
```
Now, these particular examples may well have justified a warning, but Clang also rejects
```cpp
void f(A);
void g() {
f(A(1, 1));
}
```
in which both the temporary and the aggregate live to the end of the full-expression. It also warns on
```cpp
void h(int i) {
f(A(1, static_cast<int&&>(i)));
}
```
```
<source>:16:31: warning: binding reference member 'r' to stack allocated parameter 'i' [-Wdangling-field]
f(A(1, static_cast<int&&>(i)));
^
<source>:3:9: note: reference member declared here
int&& r;
^
```
even though `i` outlives the aggregate.
This will affect uses of C++23 [`std::ranges::elements_of`](http://eel.is/c++draft/range.elementsof), which is an aggregate with a reference member that is intended to be used exactly like this.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVkGPozgT_TXOpRQEJkByyIFOJtJ3-U4r7XFk7AJ72tjINsn0ZX77ypDQ6Z7MqHfVWm2rQ8BUlV2vXr0U8151BnFPiidSHFdsDNK6fVC9t6Zb82FYNVa87A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