I know I'm missing something, and I'm hoping somebody can explain to me why this generates compiler errors:
1 2 3 4 5 6 7 8 9 10 11
template<class T>
class signal
{
vector<slot<T>*> slotvec;
voidoperator()()
{
vector<slot<T>*>::iterator pos; //ERROR
// do stuff...
};
};
The error from gcc:
error: expected `;' before ‘pos’
I've got a workaround using a non-template base_slot class and a couple of virtual methods, but I still want to know why that declaration is problematic.
Yes. This is the result of some ambiguity in the language. The ambiguity is that A::b might refer to a variable named b inside A scope (such as a namespace or a class, if b is declared static), or it might refer to a type, because class, struct, or namespace A might have a typedef of something to "b". The compiler doesn't know. In your case, it is guessing the former, which is wrong.
The fix to the problem is to tell the compiler you really mean the latter. Fortunately, this is easy to do, and here's where the keyword "typename" comes in handy:
This tells the compiler that vector<slot<T>*>::iterator is the whole type. The compiler is thinking you trying to reference a variable named "iterator" inside vector<...>, and the grammar <variable> <variable> SEMICOLON is not legal C++ syntax.
I tried it and it worked, but my testing was sloppy.
Here's what worked:
typedef vector<slot<>*>::iterator slotvec_iter;
So having slot<> (accepting defaults for all template parameters) changes it enough so the compiler can guess correctly. But you're right, with parameters it still needs the typename.