so I stumbled upon this compilation issue with Eigen. I'm using Eigen 3.3.4 and compiling on Linux with gcc 8.4.0. Basically I have a defined class inheriting from Eigen::VectorXd
which looks like this
class dVector : public Eigen::VectorXd { public: dVector() : Eigen::VectorXd() { setZero(); } dVector(int n) : Eigen::VectorXd(n) { setZero(); } dVector(const Eigen::VectorXd& v) : Eigen::VectorXd(v) {} dVector(const dVector& v) : Eigen::VectorXd(v) {} dVector& operator=(const Eigen::VectorXd& v) { Eigen::VectorXd::operator=(v); return *this; } dVector& operator=(const dVector& v) { Eigen::VectorXd::operator=(v); return *this; } ...};
And then in my code I use it like this:
static dVector a(100);// do something with adVector b = -a; // <--- won't compiledVector b = dVector(-a); <---- compiles
The second last line doesn't compile, giving your usual Eigen error message:
error: no viable conversion from 'const Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1>>::NegativeReturnType' (aka 'const CwiseUnaryOp<scalar_opposite_op<double>, const Eigen::Matrix<double, -1, 1, 0, -1, 1> >') to 'dVector'
Now the reason why I am really intrigued is that this doesn't compile on Linux, but it compiles on Windows, using MSVC.
I don't really understand why this doesn't compile, my suspicion is that it has something to do with implicit conversions or what the operation -a
actually returns; maybe it's not a dVector
but an operation tree. Haven't yet fully grasped what is happening behind the scenes in Eigen.
So then two questions:
Why doesn't
dVector b = -a;
compile ?What different thing does the MSVC compiler do that allows it to accept whatever it is that gcc refuses?