lundi 9 mars 2015

Return Value Optimization and functions that build structures

I have a function that "builds" a structure to return:



struct stuff {
int a;
double b;
Foo c;
};
stuff generate_stuff() {
Foo c = generate_foo();
//do stuff to Foo, that changes Foo:
//...
return {1, 2.0, c}; //should this be return {1, 2.0, move(c)};?
}


Should I be moving c out of the function? I realize that frequently, RVO can build the object in place, however there might be times when this ISN'T the case. When can't RVO be done, and thus, when should I move an object of a function?


Aucun commentaire:

Enregistrer un commentaire