我有這樣的代碼:默認模板參數:爲什麼編譯器抱怨沒有指定模板參數?
struct A{};
template<class T = A>
struct B {
void foo() {}
};
B b; //Error: missing template arguments before 'b'
//Error: expected ';' before 'b'
//More errors
b.foo()
如果我讓foo()
與相同的模板「簽名」模板函數,編譯器不會抱怨沒有指定模板參數:
struct A {};
struct B {
template<class T = A>
void foo() {}
};
B b; //OK
b.foo()
那麼,爲什麼我需要爲具有默認參數的模板類指定參數,但不能用於模板函數?我錯過了一些微妙的東西嗎?
原因是肯定是因爲模板參數推理失敗。但我想知道爲什麼。
這個問題的標題中是否有一個詞沒有出現? – Pointy
是的。錯過了這個詞 - 抱怨。添加它。 – badmaash
* [函數模板的默認模板參數]的可能重複(http://stackoverflow.com/questions/2447458/default-template-arguments-for-function-templates)*。 –