Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are value parameters implicitly moved when returned by value?

Tags:

Consider the following function:

Foo foo(Foo x) {     return x; } 

Will return x invoke the copy constructor or the move constructor? (Let's leave NRVO aside here.)

To investigate, I wrote a simple Foo class that is only movable but not copyable:

struct Foo {     Foo() = default;     Foo(const Foo&) = delete;     Foo(Foo&&) = default; }; 

If the move constructor were invoked when returning value parameters by value, all should be fine. But the current g++ compiler complains about return x with the following error message:

error: deleted function 'Foo::Foo(const Foo&)' 

If I replace return x with return std::move(x), everything is fine. From this I conclude that moving from value parameters must be done explicitly if desired. Is g++'s behavior conforming or not?

like image 240
fredoverflow Avatar asked May 15 '11 14:05

fredoverflow


People also ask

What is the difference between a return value and a parameter?

A return value is a result of the function's execution. It can be returned to the block of code that called the function and then used as needed. Parameters are the input for a function that are necessary for the it to be executed and produce a result. Parameters are variables defined by name and type.

What is the use of value value parameters?

Value parameters. The value parameters copy the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. This is the default mechanism for passing parameters to a method.

What happens when you pass a reference type as parameter?

This is something you'll need to pay attention to with parameters of functions. When you pass reference types as parameters, all modifications you perform on them within those functions modify the original objects: Hmm...how about parameters of the functions being constants in Java?

How to pass values to a function with parameters in Java?

This can be done by defining a function with parameters. In Java, the variable name and type of the parameter are part of function declaration. Here's what it looks like: Parameters are variables listed in the function declaration that are specified inside () by the name and type. Now you can call this function and pass any values you want to it:


2 Answers

If there is a move ctor for Foo, it should be selected.

Function parameters are explicitly excluded from copy elision in return statements (FDIS §12.9p31, first bullet):

  • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter)

However, the next paragraph explicitly brings move ctors back into consideration:

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. …

(Emphasis is mine in both quotes.)

like image 102
Fred Nurk Avatar answered Oct 20 '22 14:10

Fred Nurk


This is valid code- G++'s behaviour is non-conformant. MSVC10 does support this behaviour.

like image 28
Puppy Avatar answered Oct 20 '22 15:10

Puppy