Sample code (t0.c):
typedef struct S1 T1;
typedef T1 T[];
int main(void)
{
return sizeof(T);
}
Results:
clang (version 11.0.0):
t0.c:2:13: error: array has incomplete element type 'T1' (aka 'struct S1')
gcc (version 10.2.0):
t0.c:2:12: error: array type has incomplete element type 'T1' {aka 'struct S1'}
cl (version 19.25.28611):
t0.c(6): warning C4034: sizeof returns 0
Question: why cl
generates warning and not error?
UPD.
/Za
(Disable Language Extensions) is specified, then __STDC__
is defined with definition 1.ANSI Conformance
page (https://learn.microsoft.com/en-us/cpp/c-language/ansi-conformance?view=msvc-160):Microsoft C conforms to the standard for the C language as set forth in the 9899:1990 edition of the ANSI C standard.
The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an lvalue that designates a bitfield object.
UPD2. Added compiler versions.
Whether something is a "warning" or an ”error" is left up to the individual implementation:
5.1.1.3 DiagnosticsC 2011 Online Draft
1 A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
9) The intent is that an implementation should identify the nature of, and where possible localize, each violation. Of course, an implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated. It may also successfully translate an invalid program.
Among most compilers that I’ve used, anything that halts translation (an unrecoverable syntax error like a missing }
or an undeclared identifier) is treated as an error, whereas issues that may cause runtime errors are treated as warnings, but there’s no uniform standard for that. As long as the basic requirements above are met, the implementation is free to do what it wants. It can issue a single diagnostic of "?"
in response to all syntax and/or constraint errors and meet the requirements of the standard.