The Eigen::Map documentation here (https://eigen.tuxfamily.org/dox/classEigen_1_1Map.html) mentions the following: This class represents a matrix or vector expression mapping an existing array of data. It can be used to let Eigen interface without any overhead with non-Eigen data structures, such as plain C arrays or structures from other libraries.
- What does without any overhead mean? Does this mean that if I have an
Eigen::VectorXd
and I want to map it to astd::vector<double>
or vice versa, then since the data type is the same, we can use the same memory location for both objects?
In order to test this, I ran the following code snippet.
Eigen::VectorXd eigenVector (5);
eigenVector << 1, 1, 1, 1, 1;
std::vector<double> stdVector1 {eVector.data(), eVector.data() + eVector.size()};
std::vector<double> stdVector2 (5);
Eigen::VectorXd::Map(&stdVector2[0], eVector.size()) = eVector;
Then I printed the addresses of eigenVector, stdVector1 and stdVector2. All of them are different.
What is exactly going on behind the scenes with the last line above? :
Eigen::VectorXd::Map(&stdVector2[0], eVector.size()) = eVector;
Is the presence of an '=' leading to a copy operation to another memory location? Is there another way to use Eigen::Map that would be more efficient here?Similar question for the other way round: Mapping a
std::vector<double>
toEigen::VectorXd
. I used the following line for this -Eigen::Map<Eigen::VectorXd> eVector2(&stdVector1[0], stdVector1.size());
Same story here,eVector2
is a different memory location thanstdVector1
.Given the above, is
Eigen::Map
even useful for interfacing with raw data types? To me, it is equivalent to using something likestd::transform
instead.