Registered Member
|
Hello,
I have the following problem: I have a function f, which maps a dynamic-sized vector to another vector. For convenience, I want to allow calling f with a matrix-parameter, in which case f should be applied rowwise. As an example, consider a function f(x) that includes zeros in between the entries of x. My first try looked like this:
but then the flexibility of calling f with any expression would be lost. Best regards, Martin |
Registered Member
|
This is a classical issue of how to write templated c++ code.
The basic point is that the if() here:
needs to be evaluated much earlier. The first thing to realize is that the object x is irrelevant here so you can as well write:
I.e. only the type derived matters. So theoretically, the compiler should be able to evaluate that as soon as the type Derived is known. But unfortunately, in c++, the if() is only evaluated when your function body is getting compiler. What you want instead is a "meta if" that gets evaluated much earlier: as soon as the type "Derived" is known. This you can achieve by a different means: by template specialization. Do something like:
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]