c++ - SFINAE 和 decltype(自动)

标签 c++ auto decltype c++14

如果函数模板返回 decltype(auto)(或另一个使用 auto 的类型说明符)但返回语句格式不正确,SFINAE 会导致结果吗? return 语句是否被认为是函数签名的直接上下文?

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

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

有没有写过提案,或者有没有在任何地方正式分析过问题?

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

最佳答案

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

proposal-n3638说,

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.

希望这就是您想要的。

关于c++ - SFINAE 和 decltype(自动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17608637/

相关文章:

c++ - OpenGL GL_LINE_STRIP 在 glEnd 之后给出错误 1281(无效值)

c# - C# "var"和 C++ "auto"之间的差异

c++ - 函数标题中的箭头运算符 (->)

c++ - decltype(auto) 有哪些用途?

c++ - std::result_of 和 decltype 之间的区别

c++ - {} 在这些 C++ 函数中意味着什么?

c++ - 读取顺序文件 - 压缩文件与未压缩文件

C++11 自动。从 float 转换为 long

C++11 模板实例化错误

c# - 通过 NUnit 运行 C++ 代码