Re: [eigen] Generic functions |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
On Thursday 30 October 2008 20:34:18 Cristóvão Sousa wrote:
> Hi again,
>
> Is it possible to create a function that operates over any
> matrix type without being a template function?
This is impossible, not because of anything specific to Eigen, but because of
C++ :)
if you want your function to support n given types you can always define
explicitly these n overloads...
void foo(const MatrixXd& m);
void foo(const Matrix3d& m);
...
but if you want the compiler to understand that a function can take any type
the only way that this is possible in C++ is by making that type a template
parameter.
You can restrict to MatrixBase-derived types (includes all
matrices/vectors/xprs) by doing:
template<typename Derived> void foo(const MatrixBase<Derived>& m);
> For example, a function that accepts MatrixXd, Matrix3d,
> VectorXd, and others , and performs some operation, like the sum
> of all elements.?
>
> I think this is not possible because almost every Eigen base
> class is a template depending on the derived class, right?
Right. This would be possible if we used dynamic polymorphism (i.e. virtual
base class) instead of static polymorphism (what you describe) but that would
have a high cost in terms of performance. For example we would no longer have
compile-time-known constants, so a lot more of Eigen's logic would be
runtime.
Cheers,
Benoit
---