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

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

概述考虑这个功能模板: template<typename T>typename soft_error<T>::type foo(T, typename hard_error<T>::type){ } 从调用foo()中的第一个参数的类型推导出类型T后,编译器将继续替换T并实例化函数签名. 如果首先执行返回类型的替换,导致简单的替换失败,编译器将在计算过载集并搜索其他可行重载(SFINAE)时丢弃 考虑这个功能模板:
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所示.

总结

以上是内存溢出为你收集整理的c – 类型扣除后功能模板中的替换顺序是否有保证?全部内容,希望文章能够帮你解决c – 类型扣除后功能模板中的替换顺序是否有保证?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1247536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存