是否在decltype中执行的表达式,还是只是检查验证? [英] Is expression inside decltype executed, or just being checked for validation?
问题描述
使用 Expression SFINAE ,您可以检测是否有某些运算符或
By using Expression SFINAE, you can detect if some operator or operation is supported or not.
例如,
template <class T>
auto f(T& t, size_t n) -> decltype(t.reserve(n), void())
{ t.reserve(n); }
我的问题是 t.reserve(n)
里面 decltype
得到执行与否?
My question is that t.reserve(n)
inside decltype
get executed or not?
如果是,则表示 t.reserve(n)
执行了两次, c> decltype 而另一个在函数体内部?
If yes, does that mean t.reserve(n)
got executed twice, one inside decltype
and the other one inside the function body?
如果没有,是否只是在编译期间检查验证?
If not, is it just checked for validation during compilation time? But why it is not executed, I thought all the expressions in the comma separated expression list will get executed.
推荐答案
没有,从头到尾都没有执行,因为没有执行,我认为逗号分隔的表达式列表中的所有表达式都将被执行。 [dcl.type.simple]:
No, from [dcl.type.simple]:
decltype
说明符的操作数未经评估的操作数(第5条)。
The operand of the
decltype
specifier is an unevaluated operand (Clause 5).
表示从[expr]:
在某些上下文中,出现未求值的操作数(5.2.8,5.3.3,5.3.7,7.1.6.2)。未评估的操作数不是
计算。未完成的操作数被视为全表达式。
code> decltype(t.reserve(n),void())是验证 t.reserve(n)
有效表达式。如果是,则函数是一个可行的重载,其返回类型为void,并且 reserve()
将只调用一次(在函数体中)。如果不是,那么我们有一个替换失败,该函数不是一个可行的重载候选。
So in this particular context, the purpose of decltype(t.reserve(n), void())
is to verify that t.reserve(n)
is a valid expression. If it is, then the function is a viable overload whose return type is void, and reserve()
will be called exactly once (in the function body). If it is not, then we have a substitution failure and the function is not a viable overload candidate.
这篇关于是否在decltype中执行的表达式,还是只是检查验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!