typedef 的命名方案
我正在开发一个广泛使用结构的库,例如
typedef struct foo_bar_s {
...
} foo_bar_t;
使用 _t 后缀是一个坏主意,因为它是 POSIX 保留的命名空间。 结构体的 _s 后缀也没什么用。 所以我想我可以将其全部更改为
typedef struct foo_bar {
...
} foo_bar;
or 如果不需要结构名称
typedef struct {
...
} foo_bar;
但是,我无法再区分 typedef 和常规符号(变量等)。 这真的是一件大事吗?我应该为 typedef 使用不同的合适的命名方案吗? 或者说这并不重要?
我真的不太确定。 你怎么认为? 另外,您对 typedef 命名方案有什么建议?
I'm working on a library that extensively used constructs like
typedef struct foo_bar_s {
...
} foo_bar_t;
It's a bad idea to use the _t suffix, because it's a POSIX reserved namespace. The _s suffix for structs is also pretty useless. So I thought I can change it all to
typedef struct foo_bar {
...
} foo_bar;
or if the struct name is not needed
typedef struct {
...
} foo_bar;
However, I cannot distinguish typedefs from regular symbols (variables, etc.) anymore. Is this really such a big deal and should I use a different suitable naming scheme for the typedefs? Or does it not matter that much?
I'm really not so sure. What do you think? Also, what recommendations do you have for typedef naming schemes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管“_t”是保留的,但您不太可能遇到问题。 但是,此约定是旧版本 C 的残余,其中需要此语法才能命名结构,因此现在您可以简单地编写如下内容(省略 typedef 和 typedef 名称):
是的,您可以使用当前 C 标准中的单行注释(“//...”)。
Although, "_t" is reserved, it is very unlikely that you will encounter a problem. However, this convention is a remnant of older versions of C where this syntax was required in order to name structs, and so nowadays you can simply write something like the following (omitting the typedef and the typedef names):
And yes, you can use single line comments ("//...") in the current standard of C.
我使用 Dave Hanson 的 C 接口和实现 中使用的命名约定:类型被命名包含模块名称和大写 T。例如,序列的类型是
Seq_T
,哈希表的类型是Table_T
。 在模块的实现中,Hanson 使用#define
缩写为T
。 这基本上是应用于 C 编程的 Modula-3 约定,但我发现在短暂的初始冲击后它效果很好。I use the naming conventions used in Dave Hanson's C Interfaces and Implementations: a type is named with the module name and a capital T. So for example, the type of sequences is
Seq_T
, and the type of hash tables isTable_T
. Within the implementation of a module Hanson uses#define
to abbreviate just toT
. This is basically Modula-3 conventions applied to C programming, but I find that after a brief initial shock it works well.