Re: [eigen] odd behaviour of operator[]

[ Thread Index | Date Index | More Archives ]

Here's what I think is happening (anyone correct me if I'm wrong):

The odd behavior is not due to the square braces. It is the standard behavior of the comma operator. "states[a,0]" evaluates the expression list a,0 and then uses the last expression "0" as the evaluation result.
So "states[a,0]" is equivalent to "states[0]".

The square bracket operator cannot take multiple arguments, which is why Eigen uses parens instead.

Andre Krause wrote:
dear list,

today i coded a little code fragment and typed square braces instead of round ones:

MatrixXf states(N, 3);

for(int a=0;a < N;a++)
	states[a, 0] = sin(0.1*a);
	states[a, 1] = sin(0.2*a);
	states[a, 2] = sin(0.3*a);

oddly enough, this code compiles fine and executes without asserts, but assigns
strange values to the array. so this obviously does not work but the error is
not catched by eigen2.0.2.

of course, this way work

MatrixXf states(N, 3);

for(int a=0;a < N;a++)
	states(a, 0) = sin(0.1*a);
	states(a, 1) = sin(0.2*a);
	states(a, 2) = sin(0.3*a);

might there be a way to detect the pitfall of accessing
2d matrixes with square braces ? at least, one could disable operator[]

Mail converted by MHonArc 2.6.19+