All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Passing Eigen objects by value to functions

Passing objects by value is almost always a very bad idea in C++, as this means useless copies, and one should pass them by reference instead.

With Eigen, this is even more important: passing fixed-size vectorizable Eigen objects by value is not only inefficient, it can be illegal or make your program crash! And the reason is that these Eigen objects have alignment modifiers that aren't respected when they are passed by value.

For example, a function like this, where v is passed by value:

void my_function(Eigen::Vector2d v);
Array< int, Dynamic, 1 > v
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Code

needs to be rewritten as follows, passing v by const reference:

void my_function(const Eigen::Vector2d& v);
Code

Likewise if you have a class having an Eigen object as member:

struct Foo
{
};
void my_function(Foo v);
Code

This function also needs to be rewritten like this:

void my_function(const Foo& v);
Code

Note that on the other hand, there is no problem with functions that return objects by value.