I am confused when should I add the trailing _t
to typedef
'ed types?
我很困惑,什么时候应该向typedef类型添加末尾的_t ?
For example, should I do this:
例如,我应该这样做吗:
typedef struct image image_t;
or this:
或:
typedef struct image image;
What are the general rules?
一般规则是什么?
Another example, should I do this:
另一个例子,我应该这样做吗?
typdef enum { ARRAY_CLOSED, ARRAY_OPEN, ARRAY_HALFOPEN } array_type_t;
or this:
或:
typdef enum { ARRAY_CLOSED, ARRAY_OPEN, ARRAY_HALFOPEN } array_type;
Please enlighten me.
请开导我。
Thanks, Boda Cydo.
谢谢你,博达Cydo。
6 个解决方案
#1
54
In POSIX, names ending with _t
are reserved, so if you are targeting a POSIX system (e.g., Linux), you should not end your types with _t
.
在POSIX中,以_t结尾的名称是保留的,所以如果您的目标是POSIX系统(例如,Linux),那么您不应该以_t结束类型。
#2
17
I personally despise the _t
convention. So long as you are consistent, it really does not matter, however.
我个人鄙视这个惯例。只要你始终如一,这真的无关紧要。
Note that (as other answers here indicate) if you are coding to some other standard, such as POSIX, you need to check if it's okay in that standard before using such names.
注意,如果您正在编写其他一些标准(如POSIX),您需要检查在使用这些名称之前是否符合该标准。
#3
7
When should use use _t
? Never? It's reserved by a major standard (POSIX) and even if it's not now, your code might someday be used in a POSIX environment, so using _t
is a bad idea.
何时应该使用_t?从来没有吗?它由一个主要的标准(POSIX)保留,即使现在还没有,您的代码将来也可能在POSIX环境中使用,所以使用_t是一个坏主意。
I would go further to say that over-use of typedef
is bad in general. If your type is a struct
, union
, or enum
, use these keywords when you declare variables and it makes your code more clear. Use of typedef
is best reserved for when you want to make the underlying type invisible for abstraction/encapsulation purposes. A few great examples from standard C are size_t
, int32_t
, mbstate_t
, and the stdio FILE
.
我想进一步说,通常过度使用类型定义是不好的。如果您的类型是一个struct、union或enum,当您声明变量时使用这些关键字,它会使您的代码更加清晰。当您希望使底层类型为抽象/封装目的而不可见时,最好保留typedef的使用。标准C中的一些很好的示例是size_t、int32_t、mbstate_t和stdio文件。
Some of the worst abuses of typedef
are by the Windows API (WORD
, DWORD
, INT
, LPSTR
etc.) and glib (gint
, gchar
, etc.). Making duplicates of the standard C types with the same intended usage is only confusing and serves to lock developers into your library/platform by polluting all the code with these nonstandard type names.
一些最严重的typedef滥用是通过Windows API (WORD、DWORD、INT、LPSTR等)和glib (gint, gchar,等等)。将标准C类型的副本与预期的用途相同只会让人感到困惑,并通过使用这些非标准类型名称污染所有代码,将开发人员锁定到您的库/平台中。
#4
3
I use suffixes to increase readability: _t for typedef, and _e for enums since 25/30 years ... sometimes I use _st when typedef defines a struct.
我使用后缀来增加可读性:_t表示类型定义,_e表示25/30年以来的枚举……有时,当typedef定义一个struct时,我使用_st。
I think that is good practice to get the code be readable and standardized, then I find right to use suffixes! Furthermore, to date, I have not found any POSIX official document stating that the suffix _t is reserved.
我认为这是一种很好的做法,让代码变得可读和标准化,然后我就找到了使用后缀的权利!此外,到目前为止,我还没有找到任何POSIX官方文档说明后缀_t是保留的。
Old stdio.h contains _t ... See: grep -i "_t;" stdio.h :) I think POSIX standard is "a little" younger then C!
老的头。h包含_t……看:grep -i ' _t;h:)我认为POSIX标准比C要“年轻一点”!
#5
0
Pick good names for your types, just like you should with your variables, functions and anything else. A good name doesn't have redundant information embedded in it that makes it harder to read the code -- _t never helps you if you have a good name to begin with.
为类型选择合适的名称,就像您应该使用变量、函数和其他东西一样。一个好的名称中不包含多余的信息,这会使代码更难读懂——如果您有一个好的名称,那么它永远不会帮助您。
By the way:
typedef image image;
doesn't make any sense, since it just makes image a typedef to itself.
顺便说一句:typedef图像;这没有任何意义,因为它只是让图像本身成为一种类型定义。
#6
0
I am using _t
suffix for enums and primitive types to distinguish them from variables. I put them into namespaces, so I don't care about _t
reservations.
我使用_t后缀为枚举和基本类型区分它们与变量。我将它们放入名称空间中,所以我不关心_t保留。
To justify it. Very often variable name is an allusion to typedefed type. Like std::size_t size;
, array_t array
etc. I have found that it's easier to pick up decent name for a variable, when type contains _t
suffix. It also reminds me that it's a typedefed primitive and not some other beast like class for example.
来证明它。变量名通常是对类型类型的引用。像std::size_t大小,array_t数组等。我发现,当类型包含_t后缀时,为变量选择合适的名称会更容易。它也提醒我,它是一个类型化的原语,而不是像class这样的野兽。
#1
54
In POSIX, names ending with _t
are reserved, so if you are targeting a POSIX system (e.g., Linux), you should not end your types with _t
.
在POSIX中,以_t结尾的名称是保留的,所以如果您的目标是POSIX系统(例如,Linux),那么您不应该以_t结束类型。
#2
17
I personally despise the _t
convention. So long as you are consistent, it really does not matter, however.
我个人鄙视这个惯例。只要你始终如一,这真的无关紧要。
Note that (as other answers here indicate) if you are coding to some other standard, such as POSIX, you need to check if it's okay in that standard before using such names.
注意,如果您正在编写其他一些标准(如POSIX),您需要检查在使用这些名称之前是否符合该标准。
#3
7
When should use use _t
? Never? It's reserved by a major standard (POSIX) and even if it's not now, your code might someday be used in a POSIX environment, so using _t
is a bad idea.
何时应该使用_t?从来没有吗?它由一个主要的标准(POSIX)保留,即使现在还没有,您的代码将来也可能在POSIX环境中使用,所以使用_t是一个坏主意。
I would go further to say that over-use of typedef
is bad in general. If your type is a struct
, union
, or enum
, use these keywords when you declare variables and it makes your code more clear. Use of typedef
is best reserved for when you want to make the underlying type invisible for abstraction/encapsulation purposes. A few great examples from standard C are size_t
, int32_t
, mbstate_t
, and the stdio FILE
.
我想进一步说,通常过度使用类型定义是不好的。如果您的类型是一个struct、union或enum,当您声明变量时使用这些关键字,它会使您的代码更加清晰。当您希望使底层类型为抽象/封装目的而不可见时,最好保留typedef的使用。标准C中的一些很好的示例是size_t、int32_t、mbstate_t和stdio文件。
Some of the worst abuses of typedef
are by the Windows API (WORD
, DWORD
, INT
, LPSTR
etc.) and glib (gint
, gchar
, etc.). Making duplicates of the standard C types with the same intended usage is only confusing and serves to lock developers into your library/platform by polluting all the code with these nonstandard type names.
一些最严重的typedef滥用是通过Windows API (WORD、DWORD、INT、LPSTR等)和glib (gint, gchar,等等)。将标准C类型的副本与预期的用途相同只会让人感到困惑,并通过使用这些非标准类型名称污染所有代码,将开发人员锁定到您的库/平台中。
#4
3
I use suffixes to increase readability: _t for typedef, and _e for enums since 25/30 years ... sometimes I use _st when typedef defines a struct.
我使用后缀来增加可读性:_t表示类型定义,_e表示25/30年以来的枚举……有时,当typedef定义一个struct时,我使用_st。
I think that is good practice to get the code be readable and standardized, then I find right to use suffixes! Furthermore, to date, I have not found any POSIX official document stating that the suffix _t is reserved.
我认为这是一种很好的做法,让代码变得可读和标准化,然后我就找到了使用后缀的权利!此外,到目前为止,我还没有找到任何POSIX官方文档说明后缀_t是保留的。
Old stdio.h contains _t ... See: grep -i "_t;" stdio.h :) I think POSIX standard is "a little" younger then C!
老的头。h包含_t……看:grep -i ' _t;h:)我认为POSIX标准比C要“年轻一点”!
#5
0
Pick good names for your types, just like you should with your variables, functions and anything else. A good name doesn't have redundant information embedded in it that makes it harder to read the code -- _t never helps you if you have a good name to begin with.
为类型选择合适的名称,就像您应该使用变量、函数和其他东西一样。一个好的名称中不包含多余的信息,这会使代码更难读懂——如果您有一个好的名称,那么它永远不会帮助您。
By the way:
typedef image image;
doesn't make any sense, since it just makes image a typedef to itself.
顺便说一句:typedef图像;这没有任何意义,因为它只是让图像本身成为一种类型定义。
#6
0
I am using _t
suffix for enums and primitive types to distinguish them from variables. I put them into namespaces, so I don't care about _t
reservations.
我使用_t后缀为枚举和基本类型区分它们与变量。我将它们放入名称空间中,所以我不关心_t保留。
To justify it. Very often variable name is an allusion to typedefed type. Like std::size_t size;
, array_t array
etc. I have found that it's easier to pick up decent name for a variable, when type contains _t
suffix. It also reminds me that it's a typedefed primitive and not some other beast like class for example.
来证明它。变量名通常是对类型类型的引用。像std::size_t大小,array_t数组等。我发现,当类型包含_t后缀时,为变量选择合适的名称会更容易。它也提醒我,它是一个类型化的原语,而不是像class这样的野兽。