I am trying to write a function that takes fixed size Eigen Types (but templated on Scalar type e.g. float/double). I have read http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html but I am not able to make it work perfectly.
Here is the function definition:
template <typename T>
inline Matrix<T, 3, 3> makeSkewSymmetric(const Matrix<T, 3, 1>& v)
{
Matrix<T, 3, 3> out;
out << 0, -v[2], v[1],
v[2], 0, -v[0],
-v[1], v[0], 0;
return out;
}
Now I am using this as following:
Vector3d a(1,2,3);
Matrix3d ass = makeSkewSymmetric(a); // Compiles
Matrix3d ass = makeSkewSymmetric(a + a); // does NOT compile
I guess, I need to use some sort of MatrixBase<Derived>, but then how do I restrict the size, as the function only makes sense for vectors of length 3.
Edit: I redefined the function as following. It works, but is there a better way?
template <typename Derived>
inline Matrix<typename Derived::Scalar, 3, 3> makeSkewSymmetric(const MatrixBase<Derived>& v)
{
BOOST_STATIC_ASSERT(Derived::RowsAtCompileTime == 3 && Derived::ColsAtCompileTime == 1);
Matrix<typename Derived::Scalar, 3, 3> out;
out << 0, -v[2], v[1],
v[2], 0, -v[0],
-v[1], v[0], 0;
return out;
}
static_assertsolution, although with c++11's static assert instead of boost's... I can't think of anything else (apart from assigning the result ofa+ato anotherVector3dfirst, but of course that defeats the point of having expression templates in the first place...)