Home > Back-end >  Why Eigen doesn't need template keywords for using template function call of Matrix?
Why Eigen doesn't need template keywords for using template function call of Matrix?

Time:11-03

MWE with c 17 and Eigen 3.4.0

#include <Eigen/Dense>
using namespace Eigen;

int main()
{
  Matrix<float, 2, 2> m;
  m << 1.0, 2.0, 3.0, 4.0;
  m.cast<double>();
  // m.template cast<double>();
  return 0;
}

After reading Eigen document TopicTemplateKeyword and popular SO answer where-and-why-do-i-have-to-put-the-template-and-typename-keywords

I kinda know why/when we need template keywords. However, now I don't understand why the code above doesn't emit error message when I forget use template:

m.cast<double>();

Looks like it matches each rule of "have to use template" in the Eigen document.

  • m is a dependent name
  • cast is a member template

Why Eigen doesn't force me to add template before calling cast?

My best guess is m might not be a real dependent name(I could be wrong). It would be nice if someone could show me the source code line. I feel this is a nice FEATHER and I wanna write similar code as an author so that makes my user's life easier.

CodePudding user response:

m is not a dependent name.

You can only have dependent names inside of a template, if they depend on the template parameters of the enclosing templates.

Example:

template <typename T>
void foo()
{
  Matrix<T, 2, 2> m; // Note that `T` has to be involved.
  m << 1.0, 2.0, 3.0, 4.0;
  m.template cast<double>();
}
  • Related