Registered Member
|
Hello,
I have this piece of code
It does not compile, because:
What is the best way to work around that? Can I force evaluation of the CwiseUnaryOp to something which has actual values and can return the underlying data? Thanks! |
Moderator
|
You could do ptr = (x.array() + 1).eval().data(); but that would be useless as ptr would then immediately reference a dead object. So you need to name the temporary:
Vector3d tmp = x.array() + 1; ptr = tmp.data(); |
Registered Member
|
It also works using a static cast:
|
Moderator
|
Please don't do that !! because right after this statement pdx reference a freed memory. It might appear to work if no memory operation wrote in this buffer, but in general this won't work, and running your program within a memory debugger (e.g. valgrind) will show you the issue.
|
Registered Member
|
Ok, I see you're right.
It would be nice to have a method that force an expression template to be evaluated, so that you can call methods like data() on it. |
Moderator
|
That's the .eval() method, but you cannot use it in your context.
|
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]