4
我尝试编译使用g ++ 4.7.2如下:编译错误:无法解析重载函数类型
template <typename T>
struct A {
struct B {
T t;
template<T B::*M>
T get() {
return this->*M;
}
};
B b;
T get() {
return b.get<&B::t>();
}
};
int main() {
A<int> a;
a.get();
}
它给了我
test.cpp: In member function ‘T A<T>::get()’:
test.cpp:15:23: error: expected primary-expression before ‘)’ token
test.cpp: In instantiation of ‘T A<T>::get() [with T = int]’:
test.cpp:22:8: required from here
test.cpp:15:23: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int A<int>::B::*’ to binary ‘operator<’
为什么?
谢谢。
而当你知道C++时...我应该使用“模板消歧”? (我刚刚搜索,但我似乎无法找到这个好答案。) –
@PaulDraper:其实,我发现[这个问答](http://stackoverflow.com/questions/610245/where-and-why -do-i-have-to-put-the-template-and-typename-keywords)这应该很好地解释它。 –
非常好。这还包括'typename'的解释,我也没有完全理解。 –