问题描述
在typedef并用C VS C ++ 结构的命名空间,一个评论似乎暗示,一些暴露结构美孚
是preferable使用的typedef沿的线条。 ..
In typedef and struct namespaces in C vs C++, one of the comments seems to imply that exposing some struct foo
is preferable to using typedef along the lines of...
typedef struct foo foo;
...然后使用富
而不是结构美孚
整个API。
是否有任何缺点后者的变种?
Are there any downsides to the latter variant?
推荐答案
这要看你喜欢这个词结构
多少。如果你觉得你的程序将通过的结构是
和结构tother
宽松的洒更明确,(你不能有结构本
在C ++中,当然),然后通过各种手段使用结构
版本。
It depends how much you like the word struct
. If you feel your program will be made clearer by a liberal sprinkling of struct that
and struct tother
(you can't have struct this
in C++, of course), then by all means use the struct
version.
就个人而言,我不认为重复结构
提供任何好处,我很高兴为只使用的typedef
名称。而由于C ++有效地提供了 typedef结构XYZ XYZ;
自动声明(这是不太准确的,不只是因为你可以明确地写在C ++中,但它是足够接近你可能不必担心它),我认为这是绝对意义上使用相同的C中的C编译器是满意的,所以我通常使用 typedef结构标签标签;
,然后用标记
和标记*
需要的地方。
Personally, I don't think that repeating struct
provides any benefit and I'm happy to use just the typedef
name. And because C++ effectively provides the typedef struct xyz xyz;
declaration automatically (it isn't quite accurate, not least because you can explicitly write that in C++, but it is close enough that you probably don't have to worry about it), I think it makes perfect sense to use the same in C. The C compiler is happy with it, so I normally use typedef struct tag tag;
and then use tag
and tag *
where needed.
有关的选择,但完全站不住脚的观点,阅读指南。
For an alternative but wholly tenable view, read the Linux kernel coding style guide.
请注意,C2011可以让你重新定义一个的typedef
只要别名相同的类型:
Note that C2011 allows you to redefine a typedef
as long as it aliases the same type:
语义
¶5的声明规定了跨pretation和一组标识符的属性。一个定义
一个标识符是用于标识一个声明:
¶5 A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
- 为一个对象,导致要保留存储该对象;
— for an object, causes storage to be reserved for that object;
- 函数,包括函数体;
— for a function, includes the function body;
- 一个枚举常量,是标识符(只)申报;
— for an enumeration constant, is the (only) declaration of the identifier;
- 一个typedef名称,是标识符的第一个(或唯一的)声明
— for a typedef name, is the first (or only) declaration of the identifier.
与C99对比的地方,这是不可能的:
Contrast with C99 where this was not possible:
语义
¶5的声明规定了跨pretation和一组标识符的属性。一个定义
一个标识符是用于标识一个声明:
¶5 A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
- 为一个对象,导致要保留存储该对象;
— for an object, causes storage to be reserved for that object;
- 函数,包括函数体;
— for a function, includes the function body;
- 一个枚举常量或typedef名称,是的(唯一)的声明
标识符。
— for an enumeration constant or typedef name, is the (only) declaration of the identifier.
这只要你是一致的(但只有当你有关联的每个平台给你一个足够兼容C2011编译器)。
This simplifies the creation of type definitions as long as you're consistent (but only if you have a sufficiently compatible C2011 compiler on each platform of relevance to you).
这篇关于为什么" typedef结构富富;"认为是有害的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!