考慮下面的代碼:尚未解決重載函數類型,而調用模板成員函數的類模板
struct Test {
template <int S>
bool call();
};
template <>
bool Test::call<0>() {
return false;
}
template <>
bool Test::call<1>() {
return true;
}
template <int S, typename T>
static void func(T& t) {
t.call<S>();
}
int main()
{
Test t;
func<0>(t);
}
我得到一個編譯錯誤:
a.cpp: In function ‘void func(T&)’:
a.cpp:19:15: error: expected primary-expression before ‘)’ token
a.cpp: In instantiation of ‘void func(T&) [with int S = 0; T = Test]’:
a.cpp:25:14: required from here
a.cpp:19:5: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
如果我把t.call<0>()
或t.call<1>()
在main()
功能,它工作正常。有人能告訴我爲什麼模板論證扣除不適用於此代碼?我不確定爲什麼傳入帶有部分專用模板成員函數的類型在這種情況下不起作用。
沒有冒犯,但我不明白爲什麼這個問題有兩個upvotes。它是使用'template'關鍵字的明確例子。 – Manu343726