Namespaces
Variants
Views
Actions

Talk:cpp/language/return

From cppreference.com

Could someone expand on "Returning by value may involve construction and copy/move of a temporary object, unless copy elision is used."?

When is copy used and when move construction is used, instead?

The paragraph immediately after that sentence expands on that. "If expression is an lvalue expression and the conditions for copy elision are met or would be met...". I added some glue words if that helps. --Cubbi (talk) 11:43, 9 June 2017 (PDT)

The leading sentence

The leading sentence is problematic in that it talks about the calling function, where in the case of main there's no calling function (in the C++ program).

While technically there doesn't need to be one, in practice there always is a function that calls main. The C++ standard actually says "A function returns to its caller by the return statement." stmt.return/1 I suppose we can satisfy language-lawyers by saying "caller" here too. --Cubbi (talk) 11:41, 9 June 2017 (PDT)

Formally

Just to explain my reasoning for removing the phrase "did not select the move constructor" and replacing it with what is essentially a quote from the standard (which has now been moved into a "formally" parenthetical clause)...

"Did not select the move constructor" doesn't mean quite the same thing as what the rule actually is. The expression may still be treated as an rvalue even if overload resolution did not select the move constructor, as in this example:

struct A
{};
struct B
{
    B(const B&) = delete; // not copyable
    B(B&&) = delete; // not movable
 
    B(const A&) = delete; // forbid lvalues
    B(A&&) {} // not the move constructor
};
B foo()
{
    A a;
    return a;
}
int main() {}

Oktal (talk) 11:14, 17 July 2019 (PDT)

We could insert "which may be a converting move constructor" between "move constructor" and "formally", though I feel it's going to dilute the point even further. The chief complaint about this paragraph on cppreference has been that it doesn't say, in plain text, that return selects the move constructor (when appropriate). --Cubbi (talk) 06:13, 18 July 2019 (PDT)