lundi 2 mars 2015

Why does template method explicit template argument passing work badly with class hierarchies in C++11?

I have some code that GCC 4.9.3 with --std=c++11 will not compile:



template <class U> class super {
public:
U var;
template <class T> T a(int p){ return (T)p; }
};

template <class V> class sub: public super<V> {
public:
int foo() { return this->a<int>('a'); }
};

int main() {
sub<int> x;
return x.foo();
}


It looks like the compiler might be parsing the < in a<int> as operator< for some reason. This is the error:



subclass_templ_method.cpp: In member function 'int sub<V>::foo()':
subclass_templ_method.cpp:9:32: error: expected primary-expression before 'int'
int foo() { return this->a<int>('a'); }
^
subclass_templ_method.cpp:9:32: error: expected ';' before 'int'
subclass_templ_method.cpp:9:35: error: expected unqualified-id before '>' token
int foo() { return this->a<int>('a'); }


But, this program does compile:



template <class U> class super {
public:
U var;
template <class T> T a(int p){ return (T)p; }
};

class sub: public super<int> {
public:
int foo() { return this->a<int>('a'); }
};

int main() {
sub x;
return x.foo();
}


Why is that? Have I written ill-formed code, or is this a bug in my compiler?


Aucun commentaire:

Enregistrer un commentaire