c++ 究竟是什么是“尾随参数包”

栏目: C++ · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/41062333/what-exactly-is-a-trailing-parameter-pack
在解决功能模板重载之间的歧义时,执行部分排序(有关某些说明,请参阅 here

).在这个网站上,我们也了解到

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.

现在,我不知道什么是一个尾随的参数包.如果有的话

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

这是 CWG1395 ,最近对于C17标准草案投了一个缺陷 resolution

.以下内容添加到[temp.deduct.partial]中:

…[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 .

标准没有明确定义“尾随参数包”的含义,但是根据使用此术语的现有上下文进行判断,它是指在模板参数列表中显示为最右边参数的模板参数包:

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

这个标准缺陷报告已经存在了几年, GCCClang 都已经实施了这个决议.他们都同意上面的例子是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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Convergence Culture

Convergence Culture

Henry Jenkins / NYU Press / 2006-08-01 / USD 30.00

"Convergence Culture" maps a new territory: where old and new media intersect, where grassroots and corporate media collide, where the power of the media producer, and the power of the consumer intera......一起来看看 《Convergence Culture》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具