简体   繁体   中英

GCC optimize return value

This question is related to Function returning value vs modifying value passed by reference . I want to know if gcc will optimize the following the same way. Imagine the two functions do the same thing:

Return:

vector<int> f(...)
{
    vector<int> r;
    ...
    return r;
}

Pass by reference:

void f(vector<int> &r, ...)
{
    ...
}

vector<int> r;
f(r, ...)

Pass by reference occurs frequently in (performance critical) GMP functions.

Generally compilers are not obligated to do NRVO (Named Return Value Optimization) aka copy elision even if the certain criteria that allow it are met. Nor a program must be depended on such optimizations as it makes it effectively non portable.

Now, as far as it concerns the first case, the answer is yes, the compiler is permitted to optimize away the copy of the vector. In particular this case falls in the following wording of the standard 12.8/p31.1 Copying and moving class objects [class.copy] :

... elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

(31.1) — in a return statement in a function with a class return type, when the expression is the name of a nonvolatile automatic object (other than a function parameter or a variable introduced by the exceptiondeclaration of a handler (15.3)) with the same type (ignoring cv-qualification) as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function's return value.

Also, from C++11 and beyond std::vector has move facilities and actually the vector is going to be moved.

As far as it concerns the second case the answer is no. And after all there's no need to, since you pass a reference to the vector. That is, you pass an alias of the vector (ie the object itself).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM