2

我正在嘗試explicitly instantiate a variadic構造函數。這個最小的例子print all arguments導致我在MinGW-w64 64位Win 7上看到與GCC 5.3相同的錯誤。顯式實例化可變參數構造函數:template-id與任何模板聲明不匹配

struct stf { 
template<typename... Args> stf(Args&&... args){ 
    using expand_type = int[]; 
    expand_type{(print(args), 0)... }; 
} 
}; 

//error on next line: 
//template-id 'stf<char*, char*>' for 'stf::stf(char*, char*)' 
//does not match any template declaration 
template stf::stf<char*,char*>(char*,char*); 

回答

3

讓我們忽略的參數包,一個時刻:

template<typename Arg> stf(Arg &&args) 

突擊測驗:其實例上面的模板匹配。它是:

template<char *> stf(char *); 

template<char *> stf(char *&&); 

如果您將char *替換爲模板中的任何位置,您顯然會以第二個版本作爲正確答案。

因此,正確的模板實例必須是:

template stf::stf<char*,char*>(char* &&,char* &&); 
+0

謝謝@Sam。我試圖從構造函數轉發參數,因此我找到了一個完美的轉發示例(上面沒有鏈接),然後找到了另外兩個示例。當我把所有東西放在一起時,我沒有注意到xvalue引用不是參數包的必需特徵......並且有一個例子有它們,有一個沒有。在我的辯護中,我只是一名機械工程師,我正在回頭修復幾年前我寫的一些舊代碼:-P –

相關問題