diff --git a/content/disallow-return-temporary-glvalue.md b/content/disallow-return-temporary-glvalue.md new file mode 100644 index 0000000..502c43e --- /dev/null +++ b/content/disallow-return-temporary-glvalue.md @@ -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(r); +} +int&& f3() { + return id(42); // OK, but probably a bug +} +``` diff --git a/features_cpp26.yaml b/features_cpp26.yaml index a358780..7d3887f 100644 --- a/features_cpp26.yaml +++ b/features_cpp26.yaml @@ -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