尽管有SFINAE,该计划的形式仍然不正确吗? [英] Is this program ill-formed despite SFINAE?
问题描述
template <typename T> void f() {
return 0; // returning value from function returning `void`
}
int main()
{
// Not instantiating or calling any f<T>()
}
在对此答案的评论中,
In comments to this answer, David asserts that a function template that contains a semantic error and is not instantiated causes a program to be ill-formed:
是否使用模板都没有关系,即使没有实例化,程序也是格式错误的,但是编译器无需对其进行诊断.
Whether the template is used or not does not matter, the program is ill-formed even with no instantiation but the compiler is not required to diagnose it.
相反,我非常确定SFINAE以及防止类型推导和因此每个[C++11: 14.8.2/8]
的功能模板实例化,都可以使程序保持格式正确.但是我在此标准段落中找不到任何明确指出的文字.
Conversely, I am quite sure that SFINAE, as well as preventing type deduction and therefore instantiation of the function template per [C++11: 14.8.2/8]
, allows the program to remain well-formed. however I cannot find any text in this standard paragraph that explicitly says so.
谁是正确的?
对于这个问题,我不认为维基百科是权威的,它说的是一个稍有不同的情况:
Wikipedia, which I shall not consider authoritative for this question, says about a slightly different case:
[..] SFINAE的引入是为了避免在不相关的模板声明可见时创建格式错误的程序 [..]
(重点是我的)
推荐答案
程序根据14.6/8格式不正确:
The program is ill-formed as per 14.6/8:
如果无法为模板定义生成有效的专业化名称,并且该模板未实例化,则该模板定义格式错误,无需诊断.
If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required.
也就是说,无论您是否实例化模板,模板定义都是错误的,因为没有可能成功的实例化.
That is whether you instantiate the template or not, the template definition is ill-formed as there is no possible instantiation that will succeed.
请注意,这与SFINAE完全无关:替代失败不是错误"是替代过程的一部分,并且永远不会考虑模板的内容.
Note that this is completely unrelated to SFINAE: Substitution Failure is not an Error is part of the substitution process, and never takes into account the contents of the template.
这篇关于尽管有SFINAE,该计划的形式仍然不正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!