Archive for June 11th, 2008

Fussy C++

Posted by mikedaum on June 11th, 2008

So last night I’m all

GslMatrix D;
GslMatrix B;
C.symmetricEigs(B, D.diag() );

But the compiler is all

error: no matching function for call to 'GslMatrix::symmetricEigs(GslMatrix&, GslVector)

See the problem? D.diag() is passed in as a temporary, and the symmetricEigs has it passed in as a reference. Rather than behave rationally, the compiler refuses to consider the possibility that I meant it to pass the temporary by reference, and tells me that it can’t find a proto for what I want to do.

Now I ask you: is this good behaviour on the compiler’s part? Is this what I wanted to do? A nice error telling me that I’m trying to pass a temporary by reference would have been a little better. But is it really an error? What about side effects? I was certainly counting on those in this case, but it refused to play ball.

Anyway, I’m leaning towards my not understanding the situation. Maybe there’s really a technical or spec reason why this error is necessary. For the life of me, I can’t think of one though. So until one of you points out the essential thing that I”m missing, I’m going with this should be a warning at best, but should certainly be allowed.

Oh, and btw, the solution was to create

GslVector d;

one line above and pass that in instead. On the stack like the temporary, but somehow able to give compiler that nice cozy feeling it needed.