Concerning proper parameter type deduction in Eigen.
I want to create a class that can either be initialized with a Matrix(MatrixXd) or Vector(VectorXd). The issue is that MatrixXd and VectorXd are both typedefs in Eigen:
// roughly
typedef Matrix<double, Dynamic, 1> VectorXd;
typedef Matrix<double, Dynamic, Dynamic> MatrixXd;
If I try to have two constructors overloaded with VectorXd and MatrixXd, I get Ambiguous Constructor call error.
Consider the following example:
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
class Container {
public:
VectorXd a;
MatrixXd b;
Container(VectorXd a): a(a), b(MatrixXd::Zero(3,3)) {
cout << "Initializing with vector" << endl;
}
Container(MatrixXd b): a(VectorXd::Zero(3)), b(b) {
cout << "Initializing with matrix" << endl;
}
};
int main() {
Container x(VectorXd::Ones(4));
cout << x.a << endl;
cout << x.b << endl;
Container y(MatrixXd::Ones(4, 4));
cout << y.a << endl;
cout << y.b << endl;
return 0;
}
The exact error I get:
main.cpp:23:15: error: call to constructor of 'Container' is ambiguous
Container x(Matrix<double, Dynamic, 1>::Ones(4));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:13:5: note: candidate constructor
Container(Matrix<double, Dynamic, 1> a): a(a), b(MatrixXd::Zero(3,3)) {
^
main.cpp:17:5: note: candidate constructor
Container(Matrix<double, Dynamic, Dynamic> b): a(VectorXd::Zero(3)), b(b) {
So the question is: How could I resolve which constructor I want to call?