Are returned locals automatically xvalues

后端 未结 3 1827
南方客
南方客 2020-12-11 14:52

Following on from a comment I made on this:

passing std::vector to constructor and move semantics Is the std::move necessary in the following code, to e

3条回答
  •  感动是毒
    2020-12-11 15:37

    I think the answer is no. Though officially only a note, §5/6 summarizes what expressions are/aren't xvalues:

    An expression is an xvalue if it is:

    • the result of calling a function, whether implicitly or explicitly, whose return type is an rvalue reference to object type,
    • a cast to an rvalue reference to object type,
    • a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue, or
    • a .* pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.

    In general, the effect of this rule is that named rvalue references are treated as lvalues and unnamed rvalue references to objects are treated as xvalues; rvalue references to functions are treated as lvalues whether named or not.

    The first bullet point seems to apply here. Since the function in question returns a value rather than an rvalue reference, the result won't be an xvalue.

提交回复
热议问题