Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move semantics in Eigen

I have couple of question about Eigen:

  1. Does anyone know if there is any plan to support move semantics in Eigen anytime soon? Couldn't find anything on the TODO list of the Eigen3 web page. Right now I am using the swap trick to get rid of temporaries, like

    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'd very much like to write the above swap line in a C++11 style like

    A = foo();
    

    and not to have to worry about the temporary returned by foo().

  2. Can a C++98/C++03 compiler optimize the code A = foo(); to get rid of this temporary? Or the safest bet is to use swap()?
like image 838
vsoftco Avatar asked Nov 16 '14 17:11

vsoftco


People also ask

How do I implement move semantics on my own classes?

Implementing move semantics on your own classes is fairly simple. You’ll typically need to define a move constructor, and possibly a move assignment operator. The move constructor just transfers things in, and leaves the source object empty.

What is return value in C++ move semantics?

The return value is a rvalue reference to the object. Working of Move Semantics in C++ Whenever there is a need to move the contents of the objects between the objects instead of copying the contents from one object to another object, we make use of Move Semantics in C++.

Do I need a GitHub account to use move semantics?

To get benefits from move semantics you need some kind of resource (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, I/O streams, running threads, etc.). The source code containing the MemoryBuffer example is here (under MoveSemantics ). You do not need a Github account to download it.

What is value semantics in C++ 11?

The concept is built based on two fundamental C++ concepts: types and function/operator overloading. The following is a list of takeaway: Pre-C++ 11, value semantics sometimes lead to unnecessary and possibly expensive deep copy operations. C++ 11 introduces Rvalue references to distinguish from Lvalue references.


1 Answers

Copy elision will do the job just fine. Even a C++03 compiler will elide the temporary.
In particular, NRVO (named return value optimization) will, for this code,

MatrixXd foo()
{
    MatrixXd huge_matrix(N,N);
    return huge_matrix; 
}

MatrixXd A = foo();

construct huge_matrix right inside A. The C++03 standard specifies in [class.copy]/15:

This elision of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):

  • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value
  • when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy
like image 170
Columbo Avatar answered Oct 29 '22 10:10

Columbo