Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions content/disallow-return-temporary-glvalue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
execute: false
---

## What It Does

`&&` and `const&` references can bind to temporary objects.
With this change, this is no longer permitted when binding the result of a function
to a temporary object.

## Why It Matters

Binding the result of a function to a temporary object creates a reference that immediately dangles.
Disallowing it prevents the user from running into undefined behavior by accident.

## Example

```cpp
int&& f1() {
return 42; // error
}
const double& f2() {
static int x = 42;
return x; // error
}

auto&& id(auto&& r) {
return static_cast<decltype(r)&&>(r);
}
int&& f3() {
return id(42); // OK, but probably a bug
}
```
1 change: 1 addition & 0 deletions features_cpp26.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ features:

- desc: "Disallow binding a returned [glvalue](https://en.cppreference.com/w/cpp/language/value_category.html#glvalue) to a temporary"
paper: P2748
content: disallow-return-temporary-glvalue.md
support:
- GCC 14
- Clang 19
Expand Down