I am porting a C++ template library from VC++ to GCC.
The example below compiles fine with VS2005
#include <iostream>
template<class T>
struct A{
struct B{
template<int n>
T foo() const{
return static_cast<T>(n)/10;
}
};
T bar() const{
B b;
return b.foo<2>();
}
};
int main(){
A<float> a;
std::cout << a.bar() << std::endl;
return 0;
}
but with GCC 4.4.1 I get the following errors
g++ ttest.cpp -o ttest
ttest.cpp: In member function ‘T A<T>::bar() const’:
ttest.cpp:14: error: expected primary-expression before ‘)’ token
ttest.cpp: In member function ‘T A<T>::bar() const [with T = float]’:
ttest.cpp:20: instantiated from here
ttest.cpp:14: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
make: *** [ttest] Error 1
Could someone please explain to me what is wrong.
Is this compliant with the standard o开发者_JAVA百科r not?
You are missing a keyword, when invoking foo
- which is a function template, you have to explicitly tell GCC it's a template, with the following horrible syntax (don't ask me why they thought it was sensible).
return b.template foo<2>();
EDIT: I'm not a language lawyer, so can't tell you whether it's standards compliant or not, I'm sure one of the many on this site can! ;)
精彩评论