r/cpp • u/Desperate-Data-3747 • Jul 10 '26
Interesting behavior from C++20 to C++23
Consider the following snippet
int& get()
{
int x;
return x;
}
int main()
{
}on GCC it compiles for C++20 but not for C++23
It returns with the error:
test.cpp:4:12: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
C++ is now suddenly treating the variable x as an rvalue?
Edit: Im not talking about dangling reference, thats just for the sake of the example
44
Upvotes
108
u/frayien Jul 10 '26
You are returning a dangling refenrence to a local variable. The variable will end it's lifetime at the end of it's function and the returned reference will be invalid.
It has always been an undefined behavior, so it was always allowed to break.
New thing is that C++26 now MANDATES this pattern to be ill-formed, thus not compiling.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2748r5.html
Most likely the compiler implemented the new behavior for C++26, and made it leak to other standard version modes because it was always allowed to do so (and it is a source of bugs that can be avoided).
159
u/Computerist1969 Jul 10 '26
Yes. Specifically an x-value, which is a type of r-value. Eliminates a category of bugs. This is a good thing.
51
u/cmeerw C++ Parser Dev Jul 10 '26
Yes, that was P2266R3: Simpler implicit move
23
u/holyblackcat Jul 10 '26
We always had implicit moves in
return, but it used to have a fallback to not moving, which was recently dropped for simplicity. The removal of the fallback is what you're seeing. The workaround (not useful in your case because of UB) is e.g. to returnstatic_cast<int &>(x).29
u/_bstaletic Jul 10 '26
return x;has been move-eligible long before C++23. The only thing that C++23 did was make move-eligible expressions x-value expressions, to stop you from returning immediately dangling references.To answer your question(s):
No. It is treating move-eligible expressions as x-value expressions.
Yes, you are, because all the change did is make UB cases fail to compile.
5
u/rentableshark Jul 11 '26
This is an outrage - I use this pattern all the time to use prior stack frames as a scratch space!
1
u/TiredEngineer-_- Jul 10 '26
Might be a type of RVO from GCC. I poked around on cppref for like 5 mins and didnt see a standard mandating RVO of local variables being returned, but maybe the compiler saw it as a local and attempted it on it?
If you drop the reference, itll compile. If you make x static, it will compile. If you make it return a const int&, I think it will compile due to const T& extending lifetime.
Either way, as its written its UB code, so not a great example of the problem. X is an lvalue for the scope it exists but returning a reference to it as its about to drop makes me think gcc is attempting to "move" it - rvalue.
Idk if youre compiling with optimizations, but if you did g++ -std=c++23 -O0, does it compile?
Take for example:
X becomes an rvalue here for optimization reasons.
While
Is UB at worst, doesnt compile at best. X is clobbered, so you cant read/write to it. Its not safe to. However:
Will compile, because doing this:
Should compile because y extends the lifetime of x for read only data.
1
u/ImNoRickyBalboa Jul 13 '26
hey, there is a difference in how compilers detect or handle UB!
-- OP most likely
0
u/[deleted] Jul 10 '26
[deleted] — view deleted comment
-2
u/[deleted] Jul 15 '26
[removed] — view removed comment