This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Deducing Template Parameters From Expression Templates?

Tags: None
(comma "," separated)
betan
Registered Member
Posts
4
Karma
0
I have a templated function taking in Eigen matrices that works great,

template<typename T>
void foo(const Eigen::Matrix<var<T>, Eigen::Dynamic, 1>& v1,
const Eigen::Matrix<var<T>, Eigen::Dynamic, 1>& v2) {
...
}

In particular, T gets deduced from the arguments so calling the function
is nice and clean.

Now I was playing with extending the function to be compatible with expression
templates via http://eigen.tuxfamily.org/dox/TopicFun ... Types.html,

template<typename T, typename DerivedA, typename DerivedB>
void foo(const Eigen::MatrixBase<DerivedA>& v1,
const Eigen::MatrixBase<DerivedA>& v2) {
...
}

This works as expected, but T can no longer be deduced through the
expression templates which makes calling the function a bit of a pain.
Is there anyway to deduce T here or will the expression templates
forever hide that info?

Thanks.
jitseniesen
Registered Member
Posts
204
Karma
2
All MatrixBase<X> types have a typedef Scalar which exposes the underlying scalar. Thus, if you have
Code: Select all
typedef typename Eigen::MatrixBase<DerivedA>::Scalar Scalar;
at the top of your function you can then declare a variable of the correct type with "Scalar x;". In fact, many functions in Eigen use this.

In your case, Scalar would be var<T> so your var type needs to expect the underlying T in some way.
betan
Registered Member
Posts
4
Karma
0
Great, thanks. We were able to make this work using Eigen::MatrixBase<DerivedA>::Scalar
and some enable_if templating magic to get the signatures right.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Tip: "typename DerivedA::Scalar" is shorter.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]