C ++ class follows with template keyword
I am compiling a C ++ library that uses the C ++ math library Eigen3. However, the following codes introduce some syntax errors when compiled with VC2013:
template <typename Derived>
inline Eigen::Transform<typename Derived::Scalar, 3, Eigen::Isometry> v2t(const Eigen::MatrixBase<Derived>& x_) {
Eigen::Transform<typename Derived::Scalar, 3, Eigen::Isometry> X;
Eigen::Matrix<typename Derived::Scalar, 6, 1> x(x_);
X.template linear() = quat2mat(x.template block<3,1>(3,0));
X.template translation() = x.template block<3,1>(0,0);
return X;
}
The error messages look like this:
Error C2059 syntax error : 'template'
Error C2039 'X' : is not a member of 'Eigen::Transform<float,3,1,0>'
Error C2059 syntax error : 'template'
Error C2039 'X' : is not a member of 'Eigen::Transform<float,3,1,0>'
I've never seen codes like this X.template
, so I have no idea how I can go about doing this to fix this compilation error. Any ideas?
The keyword template
should be used here to disambiguate between the pattern and the comparison operator, for example:
struct X {
template <int A>
void f();
};
template <class T>
void g() {
T t{};
t.f<4>(); // Error - Do you want to compare t.f with 4
// or do you want to call the template t.f ?
}
You need t.template f<4>()
"disambiguate" here. The problem with the library being used is that Eigen::Transform<...>::linear
it is not a template member function, so the keyword template
is optional here and should not be used (I think).
[temps.name # 5]
The keyword-prefixed name
template
must be a template identifier, or the name must refer to a class template. [Note. The keywordtemplate
cannot be applied to members without class template templates. -end note] [...]
MSVC is right, Eigen::Transform<...>::linear
is a member without a class template template, so the keyword template
shouldn't apply. The following example from the standard should be poorly formed, but compiles fine with gcc and clang:
template <class T> struct A {
void f(int);
template <class U> void f(U);
};
template <class T> void f(T t) {
A<T> a;
a.template f<>(t); // OK: calls template
a.template f(t); // error: not a template-id
}
There is already a problem about this library regarding the library you are using on github , but without any answers from the author .. You can update the header ( ncip/bm_se3.h
) yourself or fork the project and make a fetch request to github.