7cows
7cows

Reputation: 5044

Error "Expected Expression" in this template code

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
  }
};

Upvotes: 8

Views: 7514

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126562

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>();
//^^^^^^^^

Upvotes: 20

Related Questions