2013-11-04 41 views
2

考慮下面的代碼:尚未解決重載函數類型,而調用模板成員函數的類模板

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()功能,它工作正常。有人能告訴我爲什麼模板論證扣除不適用於此代碼?我不確定爲什麼傳入帶有部分專用模板成員函數的類型在這種情況下不起作用。

+0

沒有冒犯,但我不明白爲什麼這個問題有兩個upvotes。它是使用'template'關鍵字的明確例子。 – Manu343726

回答

1

你需要使用template關鍵字的歧義解析:

template <int S, typename T> 
static void func(T& t) 
{ 
    t.template call<S>(); 
} 
2

看起來,如果你想寫

t. template call<S>(); 

名稱call明顯依賴,因此,不被認爲是除非明確指出它是模板。目前,我無法輕易檢查這是否是唯一的問題。

2

你需要說

template <int S, typename T> 
static void func(T& t) { 
    t.template call<S>(); 
} 

因爲T是一個依賴型名稱,編譯器不知道call()是一個模板函數,所以你必須要清楚。

相關問題