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

2

43

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?

Tammietammuz answered 19/12, 2021 at 10:20 Comment(2)
Does this answer your question? Are usages of incomplete types required to be diagnosed?Felder
Thanks for the reference. It is a very broad question, but one can find an answer there.Tammietammuz
M
32

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.

Mustache answered 19/12, 2021 at 10:26 Comment(0)
A
24

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.

Adown answered 19/12, 2021 at 10:31 Comment(1)
The original version of C++11 had the bug, which was fixed scant months later. Thus old news, though not yet ancient. Still, noteworthy as it applies exactly to the found compiler-bug.Shamrao

© 2022 - 2024 — McMap. All rights reserved.