chila
chila

Reputation: 2442

Cannot refer to a template name nested in a template parameter

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.

Upvotes: 8

Views: 420

Answers (2)

sbi
sbi

Reputation: 224069

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.

Upvotes: 16

James McNellis
James McNellis

Reputation: 355049

You need a typename and a template:

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

Upvotes: 8

Related Questions