我有一個模板函數C++如何定義一個函數不知道確切的參數
template <class T>
void foo() {
// Within this function I need to create a new T
// with some parameters. Now the problem is I don't
// know the number of parameters needed for T (could be
// 2 or 3 or 4)
auto p = new T(...);
}
如何解決這個問題?不知何故,我記得看到功能輸入 像(...,...)?
要查找的關鍵字:可變長參數列表,可變參數模板。 (然而,你的代碼片段似乎很奇怪,沒有什麼可以傳遞給T的構造函數的。) – us2012 2013-03-04 17:45:43
如果你不知道參數的數量,你怎麼知道要傳遞什麼值? – 2013-03-04 17:47:49
使用此鏈接 http://stackoverflow.com/questions/3307939/c-template-function-with-unknown-number-of-arguments – 2013-03-04 17:52:38