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.
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.
You need a typename
and a template
:
template <typename Provider>
inline void use()
{
typedef typename Provider::template Data<int> D;
}
精彩评论