blob: 9254fe6d88e6da635a2a0f06a23315ed1b587cee [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001namespace Eigen {
2
3/** \eigenManualPage TopicPassingByValue Passing Eigen objects by value to functions
4
5Passing 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.
6
Austin Schuhc55b0172022-02-20 17:52:35 -08007With %Eigen, this is even more important: passing \ref TopicFixedSizeVectorizable "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.
Brian Silverman72890c22015-09-19 14:37:37 -04008
Austin Schuhc55b0172022-02-20 17:52:35 -08009For example, a function like this, where \c v is passed by value:
Brian Silverman72890c22015-09-19 14:37:37 -040010
11\code
12void my_function(Eigen::Vector2d v);
13\endcode
14
Austin Schuhc55b0172022-02-20 17:52:35 -080015needs to be rewritten as follows, passing \c v by const reference:
Brian Silverman72890c22015-09-19 14:37:37 -040016
17\code
18void my_function(const Eigen::Vector2d& v);
19\endcode
20
Austin Schuhc55b0172022-02-20 17:52:35 -080021Likewise if you have a class having an %Eigen object as member:
Brian Silverman72890c22015-09-19 14:37:37 -040022
23\code
24struct Foo
25{
26 Eigen::Vector2d v;
27};
28void my_function(Foo v);
29\endcode
30
31This function also needs to be rewritten like this:
32\code
33void my_function(const Foo& v);
34\endcode
35
36Note that on the other hand, there is no problem with functions that return objects by value.
37
38*/
39
40}