Search code examples
cfunctionlanguage-lawyerdeclarationincomplete-type

Incomplete types in return-type and parameters of a function declared but not defined


Is the following code valid C? (godbolt)

typedef struct none none;
none f(none, none);

To be clear: the identifier f never appears in the translation unit again and the function itself is never defined, not even in another translation unit.


Solution

  • The C17 standard says explicitly that parameters are allowed to have incomplete types:

    (6.7.6.3 (12)) If the function declarator is not part of a definition of that function, parameters may have incomplete type and may use the [*] notation in their sequences of declarator specifiers to specify variable length array types.

    So cparser is wrong to reject the code on that basis.

    As for return types, there doesn't seem to be a clear statement either way. cppreference says "The return type of the function [...] must be a complete non-array object type or the type void" but I can't find a corresponding requirement in the standard. The standard does say in 6.9.1 (3) under "Function definitions" that "the return type of a function shall be void or a complete object type other than array type", but I read that as referring to definitions only. Likewise, 6.5.2.2(1) requires that a function being called must have a complete return type or void.

    So my opinion would be that incomplete return types are allowed in declarations, so long as the function is not defined or called. But it's hard to be certain.