Re: [eigen] Strange operator behaviour

[ Thread Index | Date Index | More Archives ]

This is because the 'then' and 'else' operands of operator?: have to be of the same type. Since the type of the 'then' operand is Transpose<MatrixXd>, the compiler tries to convert the 'else' operand to that type. Since Transpose<MatrixXd> has a constructor from MatrixXd, the compiler manages to do so silently... Making all _expression_ ctor explicit would fix such issues but also makes their use more painful...

The only solution in your case is to use a 'if-then-else' statement.


On Fri, Sep 19, 2014 at 10:37 AM, <mwb@xxxxxxxxxxxx> wrote:

I have observed a very strange behaviour using the ?: operator.

If I run the following progrom:

#include <iostream>

#include <Eigen/Core>

using namespace Eigen;

using namespace std;

int main(int argc, char* argv[])


  MatrixXd A(2,2);

  A << 11, 12,

    21, 22;

  bool flag = false;

  cout << "Matrix A:\n" << A << endl<<endl;

  MatrixXd B  = flag ? A.transpose() : A;

  cout << "Matrix B:\n" << B << endl<<endl;

  cout << "Matrix A after:\n" << A << endl<<endl;

  cout << "Flag: "<< (flag ? "true" : "false") <<endl;


I get this surprising output:

Matrix A:

11 12

21 22

Matrix B:

11 21

12 22

Matrix A after:

11 12

21 22

Flag: false

Does anybody have an idea, why B is initialized with A.transpose() instead

of A?

I am using Visual Studio 2012 Update4 and Eigen3.2.2

Does Eigen3.2.2 somewhere overrides the ?: operator with some strange side


I am very confused.


Mail converted by MHonArc 2.6.19+