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

Using blocks inside a function template

Tags: None
(comma "," separated)
gnzlbg
Registered Member
Posts
3
Karma
0
Hello everyone.

I try to use the fixed-size block expression a.leftCols<1>() inside a function template (see Example 2) and it does not compile if I do not try to instantiate the template. If I do try to instantiate it, it throws a template error from hell (remove the comment in example2() to test it!). You can see both errors below. In the documentation it says that the fixed-size block expression is equivalent to the dynamic-size block expression a.leftCols(1). However this one does work in example1 (as I think it is supposed to do). Both work if I use them in main (see code), so i guess this has to be somehow related to the fact that I am using them inside a function template. I have been fighting with this for a while already, am I doing something wrong?

I am using gcc-4.7 and Eigen 3.1.1.
The error when I do not try to instantiate it: 18:81: error: expected primary-expression before ‘)’ token
The template error from hell when I do try to instantiate it:
error: no match for ‘operator<<’ in ‘(& Eigen::operator<< <Eigen::Matrix<double, 3, 1> >((* &(& std::operator<< <std::char_traits<char> >((* & std::cout), ((const char*)"Example 2: a: ")))->std::basic_ostream<_CharT, _Traits>::operator<< <char, std::char_traits<char> >(std::endl<char, std::char_traits<char> >)), (*(const Eigen::DenseBase<Eigen::Matrix<double, 3, 1> >*)(& a))))->std::basic_ostream<_CharT, _Traits>::operator<< <char, std::char_traits<char> >(std::endl<char, std::char_traits<char> >) << a.Eigen::DenseBase<Eigen::Matrix<double, 3, 1> >::leftCols’


Code: Select all
#include <iostream>
#include <Eigen/Dense>

template <int N>
void example1() {

  Eigen::Matrix<double,N,1> a;
  // This is a minimal example, this function template should only work for N=3:
  a << 1, 2, 3;
  std::cout << "Example 1: a: " << std::endl << a << std::endl << a.leftCols(1)
  << std::endl << std::endl;
}

template <int N>
void example2() {
 
  Eigen::Matrix<double,N,1> a;
  // This is a minimal example, this function template should only work for N=3:
  a << 1, 2, 3;                                   // uncomment this wont compile
  std::cout << "Example 2: a: " << std::endl << a << std::endl //<< a.leftCols<1>()
  << std::endl << std::endl;
}

int main() {

  //! Works
  static const int N = 3;
  Eigen::Matrix<double,N,1> a;
  a << 1, 2, 3;
  std::cout << "a: " << std::endl << a << std::endl << a.leftCols<1>()
            << std::endl << std::endl;

  //! Works
  example1<N>();

  //! Fails if one uncomments: << a.leftCols<1>()
  example2<N>();
 
  return 0;
}
jitseniesen
Registered Member
Posts
204
Karma
2
This is a fairly obscure C++ issue. You need to add the template keyword:
Code: Select all
std::cout << a.template leftCols<1>();
See the explanation at http://eigen.tuxfamily.org/dox/TopicTem ... yword.html .


Bookmarks



Who is online

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