可以删除返回 C++ 中不完整类型的函数吗?

Can one delete a function returning an incomplete type in C++?

在以下示例函数中 f() 返回不完整类型 A 被标记为已删除:

struct A;
A f() = delete;

它被 GCC 接受,但不被 Clang 接受,Clang 抱怨:

error: incomplete result type 'A' in function definition

演示:https://gcc.godbolt.org/z/937PEz1h3

哪个编译器符合标准?

叮当错了

[dcl.fct.def.general]

2 The type of a parameter or the return type for a function definition shall not be a (possibly cv-qualified) class type that is incomplete or abstract within the function body unless the function is deleted ([dcl.fct.def.delete]).

我想这很清楚。删除的定义允许不完整的 class 类型。这不像函数实际上可以在格式良好的程序中调用,或者函数体实际上 使用 以某种方式不完整的类型。该函数是一个占位符,表示重载解析的结果无效。

诚然,参数 类型在实际重载解析的情况下更有趣(return 类型可以是任何类型),但没有理由也将 return 类型限制在此处完成。

一开始,9.3.4.6[dcl.fct]第9段要求

The type of a parameter or the return type for a function definition shall not be an incomplete class type (possibly cv-qualified) unless the function definition is nested within the member-specification for that class (including definitions in nested classes defined within the class).

有人提出 defect report,随后提出并追溯应用了一项决议(强调我的):

Types shall not be defined in return or parameter types. The type of a parameter or the return type for a function definition shall not be an incomplete class type (possibly cv-qualified) unless the function is deleted (9.5.3 [dcl.fct.def.delete]) or the definition is nested within the member-specification for that class (including definitions in nested classes defined within the class).

因此,Clang是错误的。