Copy elision will do the job just fine. Even the C ++ 03 compiler will be temporary.
In particular, NRVO (named return value optimization) will be for this code
MatrixXd foo() { MatrixXd huge_matrix(N,N); return huge_matrix; } MatrixXd A = foo();
build huge_matrix
right inside A
The C ++ 03 standard points to [class.copy] / 15:
This permission of copy operations is permitted in the following cases: circumstances ( which may be combined to eliminate multiple copies ):
- in a
return
expression in a function with the type of the return class, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the return type of the function, the copy operation can be omitted by creating the automatic object directly in the return value of the functions - when a temporary class object that was not attached to a link (12.2) is copied to a class object with the same cv-unqualified type, the copy operation can be omitted when constructing a temporary object directly to the target of the missed copy
source share