I am trying to write a custom conversion operator to an Eigen::Matrix2d
object, and I fail miserably. Below is the stripped-to-the-bone code:
#include <iostream>
#include <Eigen/Dense>
struct MatrixView
{
operator Eigen::Matrix2d() const // conversion operator
{
Eigen::Matrix2d tmp;
std::cout << "CONVERSION TRIGERRED\n";
return tmp;
}
};
int main()
{
MatrixView m;
static_cast<Eigen::Matrix2d>(m);
}
I am getting a nasty compile-time error, way too long to list here, starting with:
error: no matching function for call to 'Eigen::Matrix::_init1(const MatrixView&)'
note: cannot convert 'x' (type 'const MatrixView') to type 'Eigen::Index {aka long int}' Base::template _init1(x); Base::template _init1(x);
You can find the full error message here.
I have no idea what's going on, the conversion operator is trivial, it's just returning a default-initialized Eigen::Matrix2d
. Any ideas what's wrong with it?
EDIT
If I remove "explicit" then the conversion is triggered by copy-initialization, like
Eigen::Matrix2d tmp = m; // OK without "explicit"
however static_cast
still fails.
Platform details:
OS X 10.10 Yosemite, Eigen 3.2.6, g++ (MacPorts gcc5 5.2.0_0) 5.2.0, Apple LLVM version 7.0.0 (clang-700.0.72) Target: x86_64-apple-darwin14.5.0, both fail to compile the code.
EDIT 2
The whole issue happened actually because my link was pointing to a developer version of Eigen_3.3_alpha1. In Eigen 3.2.x it works. Thanks @Matt for the hint! I'll close the question.
Eigen has development packages that are produced periodically. Following the suggestion from Drop in the comments shows how to check which version is being included. There is one in alpha released on September 4th which returns version 3.2.91
. Using version 3.2.6
released on October 1st does compile correctly. The code display is the version is:
#include <Eigen\src\Core\util\Macros.h>
#include <iostream>
...
std::cout << EIGEN_WORLD_VERSION << "." << EIGEN_MAJOR_VERSION << "." <<
EIGEN_MINOR_VERSION << "\n";