Home > Enterprise >  Can one delete a function returning an incomplete type in C ?
Can one delete a function returning an incomplete type in C ?

Time:12-19

In the following example function f() returning incomplete type A is marked as deleted:

struct A;
A f() = delete;

It is accepted by GCC, but not in Clang, which complains:

error: incomplete result type 'A' in function definition

Demo: https://gcc.godbolt.org/z/937PEz1h3

Which compiler is right here according to the standard?

CodePudding user response:

Clang is wrong.

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

That's pretty clear I think. A deleted definition allows for an incomplete class type. It's not like the function can actually be called in a well-formed program, or the body is actually using the incomplete type in some way. The function is a placeholder to signify an invalid result to overload resolution.

Granted, the parameter types are more interesting in the case of actual overload resolution (and the return type can be anything), but there is no reason to restrict the return type into being complete here either.

CodePudding user response:

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.

  • Related