没有类型的 typedef 是什么意思?
What does typedef with no type mean?
来自标准 N1570 6.7.8:
A typedef
declaration does not introduce a new type, only a synonym
for the type so specified.
所以我预计不可能这样写:
typedef t;
t *t_ptr;
它应该无法编译,因为没有类型可以引入同义词。但没关系:Demo。那么这是什么意思,为什么会编译?
这取决于这样一个事实,即缺少类型规范默认为 int
。
那么,你的陈述
typedef t;
与
相同
typedef int t;
在适当的警告级别下,编译器发出警告:
warning: type defaults to ‘int’ in declaration of ‘t’ [-Wimplicit-int]
typedef t;
^
也就是说,不要依赖这种行为,"implicit int"规则自C99
以来已经过时了。
它默认为 int
。
编译器警告显示发生了什么:
#1 with x86-64 gcc 8.2
<source>:1:9: warning: type defaults to 'int' in declaration of 't' [-Wimplicit-int]
typedef t;
从 C99 开始,删除了 implicit int
规则。所以这从 C99 开始不适用。
如果你在GCC中使用-pedantic-errors
编译器选项(意味着严格符合标准),它会报错.参见 here。
如果您有兴趣,C89 标准中的 relevant section 允许这样做:
3.5.2 Type specifiers
Each list of type specifiers shall be one of the following sets; the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.
- void
- char
- signed char
- unsigned char
- short , signed short , short int , or signed short int
- unsigned short , or unsigned short int
- int , signed , signed int , or no type specifiers
所以在 C99 中,上面加粗的最后一部分(或没有类型说明符)被删除了。
typedef
声明定义对象或指针类型的同义词。因此,您应该指定要为其创建同义词的类型以及用作同义词的名称。
例如:
// 'byte_t' is a synonym for 'unsigned char'
typedef unsigned char byte_t;
// 'handler_t' is a synonym for 'void (*)(int)', a function pointer
typedef void (*handler_t)(int);
// 'short_p' is a synonym for 'short *'
typedef short * short_p;
// 'record' is a synonym for an anonymous structure
typedef struct {
int a;
char *b;
} record;
// 'error_p' is a synonym for a pointer to 'struct error', defined somewhere else
typedef struct error *error_p;
您引用的来源中还有更多示例。
来自标准 N1570 6.7.8:
A
typedef
declaration does not introduce a new type, only a synonym for the type so specified.
所以我预计不可能这样写:
typedef t;
t *t_ptr;
它应该无法编译,因为没有类型可以引入同义词。但没关系:Demo。那么这是什么意思,为什么会编译?
这取决于这样一个事实,即缺少类型规范默认为 int
。
那么,你的陈述
typedef t;
与
相同 typedef int t;
在适当的警告级别下,编译器发出警告:
warning: type defaults to ‘int’ in declaration of ‘t’ [-Wimplicit-int]
typedef t;
^
也就是说,不要依赖这种行为,"implicit int"规则自C99
以来已经过时了。
它默认为 int
。
编译器警告显示发生了什么:
#1 with x86-64 gcc 8.2
<source>:1:9: warning: type defaults to 'int' in declaration of 't' [-Wimplicit-int]
typedef t;
从 C99 开始,删除了 implicit int
规则。所以这从 C99 开始不适用。
如果你在GCC中使用-pedantic-errors
编译器选项(意味着严格符合标准),它会报错.参见 here。
如果您有兴趣,C89 标准中的 relevant section 允许这样做:
3.5.2 Type specifiers
Each list of type specifiers shall be one of the following sets; the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.
- void
- char
- signed char
- unsigned char
- short , signed short , short int , or signed short int
- unsigned short , or unsigned short int
- int , signed , signed int , or no type specifiers
所以在 C99 中,上面加粗的最后一部分(或没有类型说明符)被删除了。
typedef
声明定义对象或指针类型的同义词。因此,您应该指定要为其创建同义词的类型以及用作同义词的名称。
例如:
// 'byte_t' is a synonym for 'unsigned char'
typedef unsigned char byte_t;
// 'handler_t' is a synonym for 'void (*)(int)', a function pointer
typedef void (*handler_t)(int);
// 'short_p' is a synonym for 'short *'
typedef short * short_p;
// 'record' is a synonym for an anonymous structure
typedef struct {
int a;
char *b;
} record;
// 'error_p' is a synonym for a pointer to 'struct error', defined somewhere else
typedef struct error *error_p;
您引用的来源中还有更多示例。