I have a template class like here (in a header) with a inner class and a static member of type pointer to inner class
template <class t> class outer {
class inner {
int a;
};
static inner *m;
};
template <class t> outer <t>::inner *outer <t>::m;
when i want to define that static member i says "error: expected constructor, destructor, or type conversion before '*' token" on the last line (mingw32-g++ 3.4.5)
You need to qualify that the
inner
class is a typename, since it’s dependent on a template parameter and the C++ compiler assumes that the nameinner
in this context is not a type:Rationale: the name
inner
in the above line depends on a type name,t
. The C++ compiler at this point doesn’t know whatinner
is, because the meaning of the nameinner
can differ depending ont
. For example, suppose that, somewhere else in the code, there is a specialized version of theouter
class forint
:Now,
outer<int>::inner
no longer names a type; it names a member variable.Thus, in the general case the meaning of
outer<t>::inner
would be ambiguous and C++ resolves this ambiguity assuming thatinner
does not name a type. Unless you say it does, by prefixing it withtypename
:typename outer<t>::inner
. (In this context,inner
is called a dependent name since it depends on the exact type oft
.)