問(wèn)題描述
我想知道為什么下面的代碼不能編譯:
I am wondering why the following code doesn't compile:
struct S
{
template <typename... T>
S(T..., int);
};
S c{0, 0};
此代碼無(wú)法同時(shí)使用 clang 和 GCC 4.8 進(jìn)行編譯.這是 clang 的錯(cuò)誤:
This code fails to compile with both clang and GCC 4.8. Here is the error with clang:
test.cpp:7:3: error: no matching constructor for initialization of 'S'
S c{0, 0};
^~~~~~~
test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
S(T..., int);
^
在我看來(lái)這應(yīng)該可行,并且 T 應(yīng)該被推導(dǎo)出為長(zhǎng)度為 1 的包.
It seems to me that this should work, and T should be deduced to be a pack of length 1.
如果標(biāo)準(zhǔn)禁止做這樣的事情,有誰(shuí)知道為什么?
If the standards forbids doing things like this, does anyone know why?
推薦答案
因?yàn)楫?dāng)一個(gè)函數(shù)形參包不是最后一個(gè)形參時(shí),那么模板形參包就不能從中推導(dǎo)出來(lái),模板實(shí)參推導(dǎo)會(huì)忽略它.
Because when a function parameter pack is not the last parameter, then the template parameter pack cannot be deduced from it and it will be ignored by template argument deduction.
因此將兩個(gè)參數(shù) 0, 0
與 , int
進(jìn)行比較,結(jié)果不匹配.
So the two arguments 0, 0
are compared against , int
, yielding a mismatch.
這樣的推導(dǎo)規(guī)則需要涵蓋很多特殊情況(比如兩個(gè)參數(shù)包并排出現(xiàn)時(shí)會(huì)發(fā)生什么).由于參數(shù)包是 C++11 中的一個(gè)新特性,相應(yīng)提案的作者在起草規(guī)則時(shí)比較保守.
Deduction rules like this need to cover many special cases (like what happens when two parameter packs appear next to each other). Since parameter packs are a new feature in C++11, the authors of the respective proposal drafted the rules conservatively.
請(qǐng)注意,如果沒(méi)有以其他方式推導(dǎo)出,尾隨模板參數(shù)包將為空.所以當(dāng)你用一個(gè)參數(shù)調(diào)用構(gòu)造函數(shù)時(shí),事情就會(huì)起作用(注意這里模板參數(shù)包和函數(shù)參數(shù)包的區(qū)別.前者是拖尾的,后者不是).
Note that a trailing template parameter pack will be empty if it is not otherwise deduced. So when you call the constructor with one argument, things will work (notice the difference of template parameter pack and function parameter pack here. The former is trailing, the latter is not).
這篇關(guān)于帶有包擴(kuò)展的可變函數(shù)模板不在最后一個(gè)參數(shù)中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!