This snippet (taken from this question) compiles fine with g++ (as seen), so long the template
before the return type is there. In contrast, VC10 does not compile that code with the following error:
error C2244: 'A::getAttr' : unable to match function definition to an existing declaration
If I remove the template
, VC10 is happy but g++ screams this error:
error: non-template 'AttributeType' used as template
note: use 'A::template AttributeType' to indicate that it is a template
Is it again because of VC's broken two-phase look-up or what is the cause? Which compiler is right here? I suspect g++ to be correct, as I have a vague memory of template
being needed here, like with the rebind
template inside of allocators.
Edit: We have a winner: g++/GCC (surprise surprise...).
template <typename T, typename K>
class A {
public:
T t;
K k;
template <int i, int unused = 0>
struct AttributeType{
};
template <int i>
AttributeType<i> getAttr();
};
template <typename T, typename K>
template <int i>
typename A<T, K>::template AttributeType<i> A<T, K>::getAttr() {
// ^^^^^^^^ -- needed or not?
return t;
}
int main(){
A<int,int> a;
}
GCC is right.
AttributeType
is a dependent template-name which is followed by angle bracket<
, so the keywordtemplate
is required here to remove the ambiguity1, making it clear to the compiler that what is followed is a template-name. The rule is mentioned in §14.2/4:1 @Johannes has written a very good explanation here:
Where and why do I have to put the "template" and "typename" keywords?