本文介绍了这是一个有效的结构声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

是否有效:


struct test {


};


我的意思是,我们可以有一个不包含成员的结构吗?这是一个

不完整类型?


请提供您的专家意见。


谢谢,

Raman Chalotra

Hi All,
Is it valid:

struct test{

};

I mean, Can we have a struct containing no members? Is this a an
incomplete-type?

Please provide your expert comments.

Thanks,
Raman Chalotra

推荐答案



我没有看到声明有什么问题。虽然这些东西

看起来不像它可以有任何实际的应用程序。

标准不要求在结构中声明成员。这个

代码在Linux / gcc上用''-ansi'标志编译。

我看不出它为什么不应该有效的原因。

I don''t see anything wrong with the declaration. Though this stuff
does not look like it can have any practical applications.
The standard does not mandate declaring a member in a structure. This
code compiles with ''-ansi'' flag on Linux/gcc.
I don''t see any reason why it should not be valid.




< snip>

<snip>



我没有看到声明有什么问题。虽然这些东西

看起来不像它可以有任何实际的应用程序。

标准不要求在结构中声明成员。


I don''t see anything wrong with the declaration. Though this stuff
does not look like it can have any practical applications.
The standard does not mandate declaring a member in a structure.



我的副本有。语法指定必须至少有一个

成员。你从哪里得到你的信息?

My copy does. The syntax specifies that there must be at least one
member. Where are you getting your information from?



添加-pedantic。

Add -pedantic.



-

Ben。

--
Ben.




No.

No.



No.

No.



我没有看到声明有什么问题。


I don''t see anything wrong with the declaration.



然后你需要更仔细地看一下标准:

6.7.2.1结构和联合说明符

语法

1 struct-or-union-specifier:

struct-or-union identifieropt {struct-declaration-list}

struct-or -union标识符


struct-or-union:

struct

union


struct-declaration-list:

struct-declaration

struct-declaration-list struct-declaration

Then you need to look at the standard more carefully:
6.7.2.1 Structure and union specifiers
Syntax
1 struct-or-union-specifier:
struct-or-union identifieropt { struct-declaration-list }
struct-or-union identifier

struct-or-union:
struct
union

struct-declaration-list:
struct-declaration
struct-declaration-list struct-declaration

Note that a struct-declaration-list ALWAYS has a struct-declaration!



错误。从N1256开始:

Wrong. From N1256:



这并不能使其获得所有必需的诊断。你需要''-ansi

-pedantic'',此时它会发出警告。请注意,警告是足以满足无效标准要求的b $ b。代码(一个puch

的面孔也可以获得资格,但我不认为这个实现会这样做会非常受欢迎)

That does not make it procude all required diagnostics. You need ''-ansi
-pedantic'' at which point it gives a warning. Note that a warning is
sufficient to meet the standards requirement for "invalid" code (a puch
in the face could also qualify, but I don''t think an implementation that
did this would be very popular)



见上文。


不完整类型将是

struct test;

-

Flash Gordon

See above.

An imcomplete type would be
struct test;
--
Flash Gordon


这篇关于这是一个有效的结构声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:47