Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "Expected Expression" in this template code

Tags:

c++

templates

Why does this error appear and how do I fix it?

template<typename T>
struct foo {
  template<size_t N>
  void hello() {}
};

template<typename T>
struct bar {
  void world() {
    foo<T> f;
    f.hello<0>(); //Error: Expected expression
  }
};
like image 539
7cows Avatar asked May 12 '13 15:05

7cows


1 Answers

You need to use the template disambiguator, so the compiler will know that it shall parse hello as the name of a template member function, and the subsequent < and > as angular brackets delimiting the template arguments:

f.template hello<0>();
//^^^^^^^^
like image 165
Andy Prowl Avatar answered Nov 11 '22 18:11

Andy Prowl