c++ 究竟是什麼是“尾隨引數包”
).在這個網站上,我們也瞭解到
In case of a tie, if one function template has a trailing parameterpack and the other does not, the one with the omitted parameter isconsidered to be more specialized than the one with the emptyparameter pack.
現在,我不知道什麼是一個尾隨的引數包.如果有的話
template<class ...> struct tuple { /* ... */ }; template<class T, class...Ts> void foo(tuple<T,Ts...>); template<class T, class...Ts> void bar(T, Ts...);
是和哪些不是為什麼?請注意,cl ang考慮
template<class T> void f(tuple<T>); template<class T, class...Ts> void f(tuple<T,Ts...>); int main() {f(tuple<int>());}// ambiguous call?
含糊不清,這意味著foo沒有拖尾引數包.
.以下內容新增到[temp.deduct.partial]中:
…[if] function templateF
is at least as specialized as function templateG
and vice-versa, and ifG
has a trailing parameter pack for whichF
does not have a corresponding parameter, and ifF
does not have a trailing parameter pack, thenF
is more specialized thanG
.
標準沒有明確定義“尾隨引數包”的含義,但是根據使用此術語的現有上下文進行判斷,它是指在模板引數列表中顯示為最右邊引數的模板引數包:
template<class T, class... U> struct X; //^^^^^^^^^^
或者,在函式引數列表中顯示為最右邊引數的函式引數包:
template<class T, class... U> void y(T, U...); //^^^^
目前的草案仍然包含[temp.deduct.type]中的這個過時的例子:
template<class T, class... U> void f(T, U...); template<class T> void f(T); f(&i); // error: ambiguous
這個標準缺陷報告已經存在了幾年,GCC 和Clang 都已經實施了這個決議.他們都同意上面的例子是f的第二個過載的有效呼叫.
GCC和Clang不同意解決缺陷的範圍.這是可以理解的,因為最近才更新了包括提出的標準措辭.在您的示例中,該包不會擴充套件到函式引數列表,而是擴充套件到函式引數型別的模板引數列表中:
template<class T, class... U> void g(tuple<T, U...>); template<class T> void g(tuple<T>); g(tuple<int>{});
GCC將其視為g的第二個過載的有效呼叫; ang ang把它看作是模糊的. Clang的正確性可能取決於“尾隨引數包”是否包括尾隨的模板引數包,或僅包括尾隨函式引數包.
注意,兩個編譯器都同意C<int>是指在以下示例中類模板C的第二部分專業化:
template<class...> struct C; template<class T, class... U> struct C<T, U...> {}; template<class T> struct C<T> {};
這在Clang中似乎是一個不一致的地方,因為類模板特殊化的部分排序的標準規則是根據函式模板的部分順序來定義的.見CWG1432 .
http://stackoverflow.com/questions/41062333/what-exactly-is-a-trailing-parameter-pack