44

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

struct A;
A f() = delete;

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

error: incomplete result type 'A' in function definition

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

根据标准,哪个编译器在这里?

4

2 回答 2

33

叮当是错误的。

[dcl.fct.def.general]

2函数定义的参数类型或返回类型不应是函数体内不完整或抽象的(可能是 cv 限定的)类类型,除非函数被删除([dcl.fct.def.delete] )。

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

当然,参数类型在实际重载决议的情况下更有趣(返回类型可以是任何东西),但这里也没有理由将返回类型限制为完整。

于 2021-12-19T10:26:18.703 回答
25

At the beginning, 9.3.4.6 [dcl.fct] paragraph 9 required that

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).

A defect report was raised, and a subsequent resolution proposed and applied retrospectively (emphasis mine):

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).

Therefore, Clang is wrong.

于 2021-12-19T10:31:51.273 回答