微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 类型扣除后功能模板中的替换顺序是否有保证?

考虑这个功能模板:
template<typename T>
typename soft_error<T>::type foo(T,typename hard_error<T>::type)
{ }

调用foo()中的第一个参数的类型推导出类型T后,编译器将继续替换T并实例化函数签名.

如果首先执行返回类型的替换,导致简单的替换失败,编译器将在计算过载集并搜索其他可行重载(SFINAE)时丢弃该函数模板.

另一方面,如果首先发生第二功能参数的替换,则导致硬错误(例如由于非紧急上下文中的替换失败),整个编译将失败.

问题:对函数参数和返回类型进行替换的顺序是否有保证?

注意:This example似乎表明,在所有主要的编译器(VC11被单独测试并给出相同的结果)之后,代替返回类型的替换发生在替换参数类型之前.

解决方法

[注意:这不是原本意在作为一个自我回答的问题,而是在制定问题时我找到解决办法)

Is there any guarantee on the order in which substitution will be performed for the function parameters and return types?

不符合现行标准.

然而,this Defect Report(Xeo提供)表明,这的确是这样的.以下是C11标准第14.8.2 / 7段(已经成为n3485 draft的一部分)的拟议新措辞:

The substitution occurs in all types and expressions that are used in the function type and in template
parameter declarations. The expressions include not only constant expressions such as those that appear in
array bounds or as nontype template arguments but also general expressions (i.e.,non-constant expressions)
inside sizeof,decltype,and other contexts that allow non-constant expressions. The substitution proceeds
in lexical order and stops when a condition that causes deduction to fail is encountered
. […]

正如Nicol Bolas在对问题的评论中正确指出的那样,词法是指在参数类型之后替换尾随返回类型,如this live example所示.

原文地址:https://www.jb51.cc/c/114656.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐