New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is a variable that is about to go out of scope an lvalue or an xvalue? #4882
Comments
I'm sorry, but this is not the right venue for the discussion of how C++ works. This issues list is for editorial issues only: https://github.com/cplusplus/draft/wiki/How-to-tell-if-an-issue-is-editorial |
Stack Overflow would be more appropriate for this question. |
I did actually post the question on Stack Overflow. I eventually posted here because I suspect a conflict in the current C++ standard for the value category of returned or thrown named variables, between:
By the definition of lvalues in [basic.lval/1.4], this is impossible:
|
[class.copy.elision] allows copy/move elision for lvalues, but it doesn't mean that an eligible lvalue expression is an xvalue. It's still an lvalue, it does not change its value category. The definition of xvalue does seem unclear though. The ever-growing scope of [class.copy.elision] means that there are more and more lvalues "whose resources can be reused". |
Yes that is my point. With the current definitions of value categories, the name of an object can be an lvalue expression (its resources cannot be reused) at some point in the program, and an xvalue expression (its resources can be reused) at some other point. So the value category of a name changes. Is it something that we want? @zygoloid? |
I think the interpretation that a name (i.e. an identifier) somehow changes its value category when looked at it from different angles is wrong. [class.copy.elision] simply wants to say "apply The problem is that the definition of "xvalue" in [basic.lval] is not really needed for the standard to work; we just need a term of art to refer to certain kinds of expressions. If there is a suggestion for a more hazy definition of "xvalue" in [basic.lval], I'm all ears. |
When a variable that is about to go out of scope is returned or thrown, its resources can be reused i.e. it can be moved from, as the following C++ program shows:
Compilation (we turn off copy/move elision with the flag
-fno-elide-constructors
), linkage, and execution:clang++ -std=c++17 -O2 -Wall -pedantic -pthread -fno-elide-constructors\ main.cpp && ./a.out
Output:
x
in the above statementsreturn x;
andthrow x;
denotes an object whose resources can be reused.In the Working Draft, Standard for Programming Language C++, [basic.lval-1], we have the following definitions for value categories:
So is
x
an lvalue or an xvalue?The text was updated successfully, but these errors were encountered: