确切地说,什么是“跟踪参数包"? [英] What exactly is a "trailing parameter pack"

查看:127
本文介绍了确切地说,什么是“跟踪参数包"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解决函数模板重载之间的歧义时,将执行部分排序(请参见此处进行一些解释).在该网站上,我们也了解到了

In resolving ambiguities between function template overloads, partial ordering is performed (see here for some explanations). In that website, we also learn that

在平局的情况下,如果一个功能模板具有尾随参数 打包而另一个不打包,带有省略参数的一个是 被认为比空虚的人更专业 参数包.

In case of a tie, if one function template has a trailing parameter pack and the other does not, the one with the omitted parameter is considered to be more specialized than the one with the empty parameter pack.

现在,我想知道跟踪参数包到底是什么.如果有

Now, I wonder what precisely a trailing parameter pack is. Which if any of

template<class ...> struct tuple { /* ... */ };

template<class T, class...Ts> void foo(tuple<T,Ts...>);

template<class T, class...Ts> void bar(T, Ts...);

是,不是,为什么?另请注意,铛声认为

is and which not and why? Note also that clang considers

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没有尾随参数包.

ambiguous, implying that foo does not have a trailing parameter pack.

推荐答案

这是

This is CWG1395, for which a defect resolution was recently voted in to the draft C++17 standard. The following was added to [temp.deduct.partial]:

... [if]功能模板F至少与功能模板G一样专业,反之亦然,并且G具有尾随参数包,而F没有相应的尾随参数包参数,并且如果F没有尾随参数包,则FG更专业.

...[if] function template F is at least as specialized as function template G and vice-versa, and if G has a trailing parameter pack for which F does not have a corresponding parameter, and if F does not have a trailing parameter pack, then F is more specialized than G.

该标准未明确定义跟踪参数包"的含义,但根据使用该术语的现有上下文来判断,它指的是一个模板参数包,它在模板参数中显示为最右边的参数.列表:

The standard doesn't explicitly define what it means by "trailing parameter pack", but judging by the existing contexts in which this term is used, it refers to a template parameter pack that appears as the rightmost parameter in a template parameter list:

template<class T, class... U> struct X;
//                ^^^^^^^^^^

或者,一个功能参数包在功能参数列表中显示为最右边的参数:

Or, a function parameter pack that appears as the rightmost parameter in a function parameter list:

template<class T, class... U> void y(T, U...);
//                                      ^^^^

当前草稿仍在[temp.deduct.type]中包含此过时的示例:

The current draft still contains this outdated example in [temp.deduct.type]:

template<class T, class... U> void f(T, U...);
template<class T> void f(T);

f(&i); // error: ambiguous

此标准缺陷报告已经存在了几年,并且都 GCC C语已实现了解决方法.他们俩都同意,上面的示例是f的第二个重载的有效调用.

This standard defect report has been around for a few years, and both GCC and Clang have implemented resolutions of it. They both agree that the example above is a valid call of the second overload of f.

在缺陷解决方案的范围内,GCC和Clang意见​​分歧.这是可以理解的,因为直到最近才对其进行了更新,以包括建议的标准措辞.在您的示例中,包未扩展到功能参数列表中,而是扩展到功能参数类型的模板参数列表中:

Where GCC and Clang disagree is on the scope of the defect resolution. This is understandable, as it was only recently updated to include proposed standard wording. In your example, the pack is not expanded into the function parameter list, but into the template argument list of a function parameter type:

template<class T, class... U> void g(tuple<T, U...>);
template<class T> void g(tuple<T>);

g(tuple<int>{});

GCC将此视为对g的第二次重载的有效调用; Clang将其视为模棱两可. Clang的正确性可能取决于跟踪参数包"是要包含尾随的 template 参数包,还是仅包括尾随的 function 参数包.

GCC treats this as a valid call of the second overload of g; Clang treats it as ambiguous. The correctness of Clang may depend on whether "trailing parameter pack" is intended to include trailing template parameter packs, or only trailing function parameter packs.

请注意,两个编译器都同意C<int>在以下示例中引用类模板C的第二部分专业化:

Note that both compilers agree that C<int> refers to the second partial specialization of the class template C in the following example:

template<class...> struct C;

template<class T, class... U> struct C<T, U...> {};
template<class T> struct C<T> {};

这似乎在Clang中是不一致的,因为类模板专业化的部分排序的标准规则是根据功能模板的部分排序定义的.参见 CWG1432 .

This seems like an inconsistency in Clang, because the standard rules for partial ordering of class template specializations is defined in terms of partial ordering of function templates. See CWG1432.

这篇关于确切地说,什么是“跟踪参数包"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆