Move semantics to Eigen

I have a couple of questions about Eigen:

  • Does anyone know if there is any plan to support the semantics of relocation in Eigen anytime soon? Could not find anything on the TODO list of the Eigen3 webpage. Right now I'm using a swap trick to get rid of temporary ones, for example

     MatrixXd foo() { MatrixXd huge_matrix(N,N); // size N x N where N is quite large // do something here with huge_matrix return huge_matrix; } MatrixXd A(N, N); A.swap(foo()); 

    I would really like to write the above swap line in C ++ 11 style, for example

     A = foo(); 

    and don’t worry about temporarily returning foo() .

  • Can the C ++ 98 / C ++ 03 compiler optimize the code A = foo(); to get rid of this temporary? Or is the safest bet to use swap() ?
+6
source share
1 answer

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
+5
source

Source: https://habr.com/ru/post/978257/


All Articles