SFINAE 和 decltype(自动) [英] SFINAE and decltype(auto)

查看:37
本文介绍了SFINAE 和 decltype(自动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果函数模板返回 decltype(auto)(或使用 auto 的其他类型说明符)但返回语句格式错误,SFINAE 会产生结果吗?return 语句是否被认为是函数签名的直接上下文?

N3690 草案中似乎没有任何要求.默认情况下,我猜 SFINAE 不适用.

这看起来很不幸,因为您可以编写一个函数来转发到另一个函数,但是您不能像手写那样以委托为条件使其存在.此外,如果没有 decltype(auto),就无法检查对等非静态成员函数的存在,因为 this 不能在函数签名中使用.然而,这表明了一个基本问题,因为 decltype(auto) 提供了一个路径,可以将类类型视为成员签名中的完整类型,而事实并非如此.

提案是否已经写好,或者问题是否在任何地方进行过正式分析?

在成员签名中将类类型视为完整的能力可能有其他含义……但这只是另一个问题的素材.

解决方案

但是 return 语句格式错误,SFINAE 会产生结果吗?

提案-n3638说,

<块引用>

SFINAE

由于返回类型是通过实例化模板推导出来的,如果实例化格式错误,这将导致错误而不是替换失败.这允许自动函数返回一个 lambda,而使用 decltype(returned expression) 模式是不可能的.

希望这就是您正在寻找的.

If a function template returns decltype(auto) (or another type specifier using auto) but the return statement would be ill-formed, does SFINAE result? Is the return statement considered to be the immediate context of the function signature?

Nothing in the N3690 draft seems to require this. By default, I guess SFINAE does not apply.

This seems unfortunate because you can write a function to forward to another function, but you cannot make its existence conditional on the delegate as when writing longhand. Furthermore, checking the existence of a peer nonstatic member function cannot be done without decltype(auto) because this cannot be used in a function signature. However this indicates a fundamental problem, as decltype(auto) provides a path to considering the class type as complete within a member signature, where it's not.

Has a proposal been written, or has the problem been formally analyzed anywhere?

The ability to treat the class type as complete within a member signature may have other implications… but that's just fodder for another question.

解决方案

but the return statement would be ill-formed, does SFINAE result?

The proposal-n3638 says,

SFINAE

Since the return type is deduced by instantiating the template, if the instantiation is ill-formed, this causes an error rather than a substitution failure. This allows an auto function to return a lambda, which is not possible using the decltype(returned expression) pattern.

Hope that is what you're looking for.

这篇关于SFINAE 和 decltype(自动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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