从这个问题延伸出来
我无法理解这段代码.
struct foo myfoo; // --> Is it forward declaration or object creation. ?
struct foo
{
int a;
};
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在标记为箭头的代码中-->是前向声明还是对象创建.?
如果那是前向声明那么struct foo;叫什么?如果它是对象创建或实例化,那么它如何在结构定义之前创建对象.
在gcc编译器上它工作正常,但其他编译器给出错误.
gcc -Werror -Wall tst.c -o tst
Run Code Online (Sandbox Code Playgroud)
有关此行为的任何建议或解释gcc?我无法在任何地方找到它.
Pra*_*rav 16
看起来像暂定的定义,myfoo并且因为提供了结构的定义,所以没有错误.
clang在未定义类型时提供全面的诊断.
prasoon@ats-VPCEB3AGG:~$ cat tst.c
struct foo myfoo;
//struct foo{
// int x ;
//} ;
int main()
{
}
prasoon@ats-VPCEB3AGG:~$ clang tst.c
tst.c:1:12: error: tentative definition has type 'struct foo' that is never
completed
struct foo myfoo;
Run Code Online (Sandbox Code Playgroud)
我不认为它是一个gcc bug,clang以及comeau online正在编译代码.
$ 6.9.2/2
具有没有初始化程序的文件范围且没有存储类说明符或存储类说明符为静态的对象的标识符声明构成暂定定义.如果翻译单元包含一个或多个标识符的暂定定义,并且翻译单元不包含该标识符的外部定义,那么行为就像翻译单元包含该标识符的文件范围声明一样,复合类型为翻译单元的结尾,初始化程序等于0.