c++ - Xvalues vs prvalues: what does identity property add -
i'm sorry broadness of question, it's these details tightly interconnected..
i've been trying understand difference between 2 value categories - xvalues , prvalues, still i'm confused.
the property of having identity (which makes xvalue different prvalue) discussed in following question on so:
is correct xvalues have identity , movable?
first of all, accepted answer quotes ยง5/7 definition of xvalue; cppreference (http://en.cppreference.com/w/cpp/language/value_category) provides broader definition of xvalue - if consider 2 latter cases 5/7, according cppreference (and compilers know well) first expression can generic rvalue, not xvalue. source more correct here?
anyway, mental model tried develop myself notion of 'identity' expression has should guaranteed reside in actual program's data memory.
like reason string literals lvalues, they're guaranteed reside in memory entire program run, while number literals prvalues , e.g. hypothetically stored in straight asm.
the same seems apply std::move
prvalue literal, i.e. when calling fun(1)
parameter lvalue in callee frame, when calling fun(std::move(1))
xvalue 'kind' of glvalue must kept in caller frame.
however mental model doesn't work @ least temporary objects, which, understand, should created in actual memory (e.g. if rvalue-ref-taking func called fun(myclass())
prvalue argument). guess mental model wrong.
so correct way think 'identity' property of xvalues? i've read identity can compare addresses if compare addresses of 2 myclass().member
s (xvalue according cppreference), let's passing them rvalue refs comparison function, don't understand why can't same 2 myclass()
s (prvalue)?
one more source that's connected answer here: what move semantics?
note though std::move(a) rvalue, evaluation not create temporary object. conundrum forced committee introduce third value category. can bound rvalue reference, though not rvalue in traditional sense, called xvalue (expiring value).
but seems have nothing 'can compare addresses' , a) don't see how different 'traditional sense' of rvalue; b) don't understand why such reason require new value category in language (well, ok, allows provide dynamic typing objects in oo sense, xvalues don't refer objects).
i have mental model doesn't deal directly identity , memory , whatnot.
prvalue
comes "pure rvalue" while xvalue
comes "expiring value" , information use in mental model:
pure rvalue refers object temporary in "pure sense": expression compiler can tell absolute certainty evaluation object temporary has been created , expiring (unless intervene prolong it's lifetime reference binding). object created during evaluation of expression , die according rules of "mother expression".
by contrast, expiring value expression evaluates reference object promised expire soon. gives promise can whatever want object because destroyed next anyway. don't know when object created, or when supposed destroyed. know "intercepted" die.
in practice:
struct x; auto foo() -> x;
x x = foo(); ^~~~~
in example evaluating foo()
result in prvalue
. looking @ expression know object created part of return of foo
, destroyed @ end of full expression. because know of these things can prologue it's lifetime:
const x& rx = foo();
now object returned foo has it's lifetime prolongued lifetime of rx
auto bar() -> x&&
x x = bar(); ^~~~
in example evaluating bar()
result in xvalue
. bar
promises giving object expire, don't know when object created. can created way before call bar
(as temporary or not) , bar
gives rvalue reference
it. advantage know can whatever want because won't used afterwords (e.g. can move it). don't know when object supposed destroyed. such cannot extend it's lifetime - because don't know original lifetime in first place:
const x& rx = bar();
this won't extend lifetime.
Comments
Post a Comment