c - cl : array type has incomplete element type => why warning and not error?

标签 c visual-c++ compiler-errors incomplete-type cl

示例代码(t0.c):

typedef struct S1 T1;
typedef T1 T[];

int main(void)
{
    return sizeof(T);
}
结果:
clang(11.0.0版):
t0.c:2:13: error: array has incomplete element type 'T1' (aka 'struct S1')
gcc(版本10.2.0):
t0.c:2:12: error: array type has incomplete element type 'T1' {aka 'struct S1'}
cl(版本19.25.28611):
t0.c(6): warning C4034: sizeof returns 0
问题:为什么cl生成警告而不是错误?
UPD。
  • 如果指定了/Za(禁用语言扩展),则使用定义1定义__STDC__
  • 根据ANSI Conformance页面(https://docs.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.


  • ISO/IEC 9899:1990,6.3.3.4 sizeof运算符(添加了强调):

  • 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。添加了编译器版本。

    最佳答案

    究竟是“警告”还是“错误”取决于具体实现:

    5.1.1.3 Diagnostics
    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.

    C 2011 Online Draft
    在我使用的大多数编译器中,任何停止翻译的操作(不可恢复的语法错误,如缺少}或未声明的标识符)都被视为错误,而可能导致运行时错误的问题则被视为警告,但没有统一的标准为了那个原因。只要满足上述基本要求,实现就可以自由地执行其想要的操作。它可以响应于所有语法和/或约束错误而发出"?"的单个诊断,并满足标准的要求。

    关于c - cl : array type has incomplete element type => why warning and not error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65461839/

    相关文章:

    c++ - 我的 C 文件是由 Pro*C 生成的吗

    c++ - C 和 C++ 中几乎相同的代码在执行时间上的巨大差异 (x9)

    c# - 如何使用 pinvoke 将图像从 c 或 c++ 发送到 C#

    gcc - 编译器在 Fedora 28 上缺乏 asm-goto 支持

    c - 如何在 Linux 工作中使用 "make and building executables"

    c - 缓冲区溢出(返回地址)

    c - c代码中的gcc和llvm linux关闭功能

    c++ - 有没有办法验证另一个程序的当前窗口是否完全可见?

    c++ - 如何在 Visual Studio 汇编程序输出中分解名称?

    c++ - 链接C++程序时出错