template<class T>
class MyClass
{
public:
myMethod();
};
template<class T>
void MyClass<T>::myMethod()
{
vector<string>::iterator it1;
vector<T*>::iterator it2; //error happens here
//More code here
}
Xcode + template pointer vector iterator = error?
The problem is that vector<T>::iterator is a dependant type, the standard requires that you use the typename keyword to inform the compiler of this. Visual C++ obviously accepts code without this keyword, it is not a weakness in Xcode - which AFAIK uses GCC as its compiler.
Google the emphasised terms (along with "C++") to get more information for why this is necessary.
template<class T>class MyClass{public: myMethod();};template<class T>void MyClass<T>::myMethod(){ vector<string>::iterator it1; typename vector<T*>::iterator it2; //More code here}
Google the emphasised terms (along with "C++") to get more information for why this is necessary.
TO back up what rip-off said:
The type being dependent means that the type T is dependent on a template parameter. For all the compiler knows, vector<T*>::iterator is actually a static variable. typename assures it it's not. vector<T*> may or may not be dependent (I don't know) but vector is known from global scope to be a class so there's no issue.
G++ is more strict in correctness here, and most places it seems, about not assuming that ::iterator is a type. I suppose this is to enforce proper, portable code.
vector<string>::iterator it1; // Independent type. (Although, the actual term is, *ahem* non-dependent.)vector<T*>::iterator it2; // Dependent type.
The type being dependent means that the type T is dependent on a template parameter. For all the compiler knows, vector<T*>::iterator is actually a static variable. typename assures it it's not. vector<T*> may or may not be dependent (I don't know) but vector is known from global scope to be a class so there's no issue.
G++ is more strict in correctness here, and most places it seems, about not assuming that ::iterator is a type. I suppose this is to enforce proper, portable code.
I appreciate the swift and thorough response, guys. This is why I need to explore the realm of GNU more often. I understand completely and it fixed up my errors. Thanks again.
Quote:vector<T*> may or may not be dependent (I don't know) but vector is known from global scope to be a class so there's no issue.
It's not, for the record.
Quote:G++ is more strict in correctness here, and most places it seems, about not assuming that ::iterator is a type. I suppose this is to enforce proper, portable code.
Yep. I remember the days GCC didn't require it. The last time I'd used it, it didn't care about dependent template-name syntax, though, like MSVC did at the time -- although now it does.
What was once: std::allocator<T>::rebind<U>::type::rebind<V>::typeIs now: typename std::allocator<T>::template rebind<U>::type::template rebind<V>::type
Isn't C++ fun?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement