在类型扣除以后,在功能模板中的替换顺序是否有任何保证? [英] Is there any guarantee on the order of substitution in a function template after type deduction?

查看:135
本文介绍了在类型扣除以后,在功能模板中的替换顺序是否有任何保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此函数模板:

template<typename T>
typename soft_error<T>::type foo(T, typename hard_error<T>::type)
{ }

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

After deducing type T from the type of the first argument in the call to foo(), the compiler will proceed to substitute T and instantiate the function signature.

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

If substitution for the return type gets executed first, causing a simple substitution failure, the compiler will discard this function template when computing the overload set and search for other viable overloads (SFINAE).

另一方面,如果第二个函数参数的替换首先发生,导致硬错误(例如,由于非立即上下文中的替换失败),整个编译都会失败。

On the other hand, if substitution for the second function parameter occurs first, causing a hard error (e.g. because of a substitution failure in a non-immediate context), the entire compilation would fail.

问题: 函数参数和返回类型?

http://liveworkspace.org/code/4cvdpz%2464\">此示例似乎表明在所有主要编译器(VC11被单独测试并给出相同的结果)替换返回类型发生在代替参数之前类型。

NOTE: This example seems to show that on all major compilers (VC11 was tested separately and gave identical results) substitution for the return type occurs before substitution for the parameter types.

推荐答案

[注意:这本来不是一个自我回答的问题,在制作问题时找出解决方案]

[NOTE: This was not originally meant to be a self-answered question, but I happened to find out the solution while crafting the question]


将对函数参数和返回类型执行替换的顺序?

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

不在当前标准中。 / strong>

Not in the current standard.

但是, 此缺陷报告 (由 Xeo 提供)显示,这确实是为了是的情况。下面是C ++ 11标准(已成为 n3485草案):

However, this Defect Report (courtesy of Xeo) shows that this is indeed intended to be the case. Here is the proposed new wording for Paragraph 14.8.2/7 of the C++11 Standard (which has become part of the n3485 draft):


替换发生在所有类型和表达式中在函数类型和模板
中使用参数声明。表达式不仅包括常量表达式,例如出现在
数组边界或非类型模板参数中的常量表达式,还包括一般表达式(即非常量表达式)
在sizeof,decltype和其他上下文中,允许非常量表达式。 替换按照词汇顺序进行
,并在遇到导致失败的情况时停止
。 [...]

正如 Nicol Bolas 对问题的评论中,词汇顺序意味着尾随返回类型将在之后替换参数类型,如图所示在此实例中。

As correctly pointed out by Nicol Bolas in the comments to the question, lexical order means that a trailing return type would be substituted after the parameter types, as shown in this live example.

这篇关于在类型扣除以后,在功能模板中的替换顺序是否有任何保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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