I had a really new but very interesting compiling issue these days. Let's see the following construct:
#include <iostream>
#include <vector>
//------------------------------------------------------------------
template<class T>
class A {
public:
A();
};
//------------------------------------------------------------------
template<class T>
A<T>::A()
{
std::vector<T*>::iterator iter;
}
//------------------------------------------------------------------
int main() {
std::cout << "Test template issues" << std::endl;
A<unsigned> a;
return( 0 );
}
That's what I'd expect to work and it even would with some compilers
but not with gcc. The problem here is the question if
std::vector<T*>::iterator is a type, a variable (member) or a
function (method). We get the following error:
In constructor ‘A<T>::A()’:
error: expected `;' before ‘iter’
In constructor ‘A<T>::A() [with T = unsigned int]’:
error: dependent-name ‘std::vector<T*,std::allocator<T*>
>::iterator’ is parsed as a non-type, but instantiation yields
a type
note: say ‘typename std::vector<T*,std::allocator<T*>
>::iterator’ if a type is meant
The default behaviour is that the parser assumes it to be a non-type
(he expects a member or a method). But during instantiation it proves
to be wrong - std::vector<T*>::iterator is a type.
The situation is easy to resolve if we read the error message
We have to declare the iterator like:
typename std::vector<T*>::iterator iter;
After knowing what problem I have I also found the right chapter in Stroustrup's bible at
C.13.5 Typename and Template (The C++ Programming Language - Bjarne Stroustrup)