SFINAE and decltype(auto)

家住魔仙堡 提交于 2019-11-28 10:48:55

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.

Following up on Nawaz's link, the remaining questions are answered by N3690 §7.1.6.4/11:

If the type of an entity with an undeduced placeholder type is needed to determine the type of an expression, the program is ill-formed.

This means that even if SFINAE worked with return type deduction, it couldn't be used to query one function declaration from another. The signature is essentially invalid until the return statement is processed, which occurs at the closing brace of the class {} definition, and after the definitions of preceding members have been processed.

In a sense, all member decltype(auto) functions are incomplete with respect to preceding functions in the same class:

struct s {
    void f() { a(); } // error: use of ‘auto s::a()’ before deduction of ‘auto’
    auto a() { return 3; }
};

This is GCC's complaint; it goes away if the member declarations are reversed. This is because the function definitions are processed in order of declaration, when the } from the class definition is reached. If the statement a(); is processed before the return 3;, then the program is ill-formed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!