8

I have the following code:

template <typename Provider>
inline void use()
{
    typedef Provider::Data<int> D;
}

Where I'm basically trying to use a template class member 'Data' of some 'Provider' class, applied to 'int', but I get the following errors:

util.cpp:5: error: expected init-declarator before '<' token
util.cpp:5: error: expected `,' or `;' before '<' token

I'm using GCC 4.3.3 on a Solaris System.

chila
  • 2,372
  • 1
  • 16
  • 33
  • possible duplicate of [C++ template member function of template class called from template function](http://stackoverflow.com/questions/1840253/c-template-member-function-of-template-class-called-from-template-function) – Kirill V. Lyadvinsky May 18 '10 at 19:38
  • 1
    @Kirill: I think this one is unique in that it requires both `typename` and `template`. (I struggled for a while with how to explain this using the same code and I'm not satisfied.) – sbi May 18 '10 at 19:43
  • @sbi, yes, this is not exact the same, but very similar question. – Kirill V. Lyadvinsky May 18 '10 at 19:47

2 Answers2

16
typedef typename Provider::template Data<int> D;

The problem is that, when the compilers parses use() for the first time, it doesn't know Provider, so it doesn't know what Provider::Data refers to. It could be a static data member, the name of a member function or something else. That's why you have to put the typename in.
The additional template is necessary whenever the nested name is the name of a template. If it was something else, then Data < ... could be a comparison.

sbi
  • 219,715
  • 46
  • 258
  • 445
8

You need a typename and a template:

template <typename Provider>
inline void use()
{
    typedef typename Provider::template Data<int> D;
}
James McNellis
  • 348,265
  • 75
  • 913
  • 977