T is not a class but it is

Why this doesn't work? (For hell's sake!)

template<class T>
class A
{
    typedef typename T::value_type value_type;
public:
    A();
};

I'm getting following error:
Error 1 error C2825: 'T': must be a class or namespace when followed by '::

But T is a class, I've just specified that didn't I? So what's the problem?
Thanks.

Upvotes: 0

Views: 291

Answers (4)

usta
usta

Reputation: 6869

The 'class' keyword has a different meaning when used to specify a template type parameter. In fact, template<class T> and template <typename T> are completely equivilent, and T can be just about any type. Writing template<class T> in no way tells the compiler that T shall be only a class type.

Upvotes: 2

Razzupaltuff
Razzupaltuff

Reputation: 2311

Just FYI: Visual C++ only throws such an error if encountering a related problem when actually creating a concrete class from a template. You should be able to determine where that happened rather easily from the error message, and looking into that code might get you a long way to fixing such problems.

Upvotes: 0

jbernadas
jbernadas

Reputation: 2600

In which template specialization are you getting this error? Maybe you are doing something like A<int> somewhere in the code. Please give more information about the specialization that gives the error if you want better information.

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272687

T could be a primitive type, depending on how you instantiate the template...

Upvotes: 7

Related Questions