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


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

查看所有标签

猜你喜欢:

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

机器学习

机器学习

周志华 / 清华大学出版社 / 2016-1-1 / 88.00元

机器学习是计算机科学与人工智能的重要分支领域. 本书作为该领域的入门教材,在内容上尽可能涵盖机器学习基础知识的各方面。 为了使尽可能多的读者通过本书对机器学习有所了解, 作者试图尽可能少地使用数学知识. 然而, 少量的概率、统计、代数、优化、逻辑知识似乎不可避免. 因此, 本书更适合大学三年级以上的理工科本科生和研究生, 以及具有类似背景的对机器学 习感兴趣的人士. 为方便读者, 本书附录给出了一......一起来看看 《机器学习》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具