Suppose I want to know whether a type is an Eigen
object, as opposed to a primitive or some other class. How do I achieve this without actually casting the object?
template <typename Derived>
EigenBase<Derived> fun(const EigenBase<Derived>& value)
{
// This is bad, because value is now of type EigenBase<Derived> and I don't know
// what the original type is.
return value;
}
template <typename T>
T fun(const T& value)
{
// This will also catch primitives and other classes.
return value;
}
How can I catch only types which can be type-deduced to Eigen::EigenBase<T>
, without actually casting them to that type?
In the first version, the actuel expression type is Derived
, and you can get it via the .derived()
member:
template <typename Derived>
typename Derived::PlainObject fun(const EigenBase<Derived>& a_value)
{
const Derived &value(a_value.derived()); // the actual expression object
typename Derived::PlainObject ret; // this is an object with storage, typically the Matrix<> type closest to Derived.
ret = 2*value;
return ret;
}
Moreover you should never returns by value, and more generally create, an EigenBase<> object. EigenBase has no storage at all, it is like an abstract base class.