我们可以在声明的任何地方放置“typedef”说明符吗?(复制)

时间:2021-01-19 13:38:22

This question already has an answer here:

这个问题已经有了答案:

The syntax of typedef specifier :

类型定义说明符的语法:

typedef <existing_name> <alias_name>

for example:

例如:

typedef long unsigned int Int;

It's working fine.

这是工作正常。

But, If I place typedef anywhere in the declaration, Like this:

但是,如果我把typedef放在声明的任何地方,比如:

long unsigned typedef int Int;

Then, It's also working fine.

然后,它也运行得很好。

Why? Can we place typedef anywhere in the declaration?

为什么?我们可以在声明的任何地方放置typedef吗?

2 个解决方案

#1


17  

First of all, quoting from §6.11.5, "Future language directions"

首先,引用§6.11.5,“未来语言方向”

1 The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

声明中除了声明说明符开头之外,存储类说明符的位置是一个过时的特性。

So, do not rely on this, as it may be removed in the future.

所以,不要依赖它,因为它可能在未来被移除。


That said, to understand why this works, check the C11 standard, chapter §6.7.2:

也就是说,要理解其工作原理,检查C11标准,章§6.7.2:

[...] the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

[…类型说明符可以以任何顺序出现,可能与其他声明说明符混合。

From §6.7.1, we know typedef is a storage-class specifier (one particular sort of a declaration specifier), so it can be placed after (or before) the type specifier (i.e., can be intermixed). It does not change anything.

从§6.7.1,我们知道typedef是存储类说明符(一个特定的一种宣言说明符),所以它可以被放置在类型说明符(即(或之前)。,可以混在一起)。它不会改变任何东西。

#2


7  

This is indeed allowed by the C standard. typedef is a storage class specifier and if you look at the grammar given in the C standard (N1570, latest draft for C11, §6.7 p1):

这确实是C标准允许的。typedef是存储类说明符,如果你看一下语法C标准中给出(C11 N1570,最新草案,§6.7 p1):

Syntax
declaration:
. declaration-specifiers init-declarator-list(opt) ;
. static_assert-declaration
declaration-specifiers:
. storage-class-specifier declaration-specifiers(opt)
. type-specifier declaration-specifiers(opt)
. type-qualifier declaration-specifiers(opt)
. function-specifier declaration-specifiers(opt)
. alignment-specifier declaration-specifiers(opt)
init-declarator-list:
. init-declarator
. init-declarator-list , init-declarator
init-declarator:
. declarator
. declarator = initializer

声明:语法。declaration-specifiers init-declarator-list(选择);。static_assert-declaration declaration-specifiers:。storage-class-specifier declaration-specifiers(选择)。type-specifier declaration-specifiers(选择)。type-qualifier declaration-specifiers(选择)。function-specifier declaration-specifiers(选择)。alignment-specifier declaration-specifiers(选择)init-declarator-list:。init-declarator。申报人员名单,申报人员名单。说明符。说明符=初始化

the storage class specifier can appear after other declaration specifiers like the type specifier.

存储类说明符可以出现在其他声明说明符(如类型说明符)之后。

But you shouldn't use it, it's obsolescent, see §6.11.5:

但是你不应该使用它,这是荒废的,看到§6.11.5:

The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

声明中除了声明说明符开头之外的存储类说明符的位置是一个过时的特性。

#1


17  

First of all, quoting from §6.11.5, "Future language directions"

首先,引用§6.11.5,“未来语言方向”

1 The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

声明中除了声明说明符开头之外,存储类说明符的位置是一个过时的特性。

So, do not rely on this, as it may be removed in the future.

所以,不要依赖它,因为它可能在未来被移除。


That said, to understand why this works, check the C11 standard, chapter §6.7.2:

也就是说,要理解其工作原理,检查C11标准,章§6.7.2:

[...] the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

[…类型说明符可以以任何顺序出现,可能与其他声明说明符混合。

From §6.7.1, we know typedef is a storage-class specifier (one particular sort of a declaration specifier), so it can be placed after (or before) the type specifier (i.e., can be intermixed). It does not change anything.

从§6.7.1,我们知道typedef是存储类说明符(一个特定的一种宣言说明符),所以它可以被放置在类型说明符(即(或之前)。,可以混在一起)。它不会改变任何东西。

#2


7  

This is indeed allowed by the C standard. typedef is a storage class specifier and if you look at the grammar given in the C standard (N1570, latest draft for C11, §6.7 p1):

这确实是C标准允许的。typedef是存储类说明符,如果你看一下语法C标准中给出(C11 N1570,最新草案,§6.7 p1):

Syntax
declaration:
. declaration-specifiers init-declarator-list(opt) ;
. static_assert-declaration
declaration-specifiers:
. storage-class-specifier declaration-specifiers(opt)
. type-specifier declaration-specifiers(opt)
. type-qualifier declaration-specifiers(opt)
. function-specifier declaration-specifiers(opt)
. alignment-specifier declaration-specifiers(opt)
init-declarator-list:
. init-declarator
. init-declarator-list , init-declarator
init-declarator:
. declarator
. declarator = initializer

声明:语法。declaration-specifiers init-declarator-list(选择);。static_assert-declaration declaration-specifiers:。storage-class-specifier declaration-specifiers(选择)。type-specifier declaration-specifiers(选择)。type-qualifier declaration-specifiers(选择)。function-specifier declaration-specifiers(选择)。alignment-specifier declaration-specifiers(选择)init-declarator-list:。init-declarator。申报人员名单,申报人员名单。说明符。说明符=初始化

the storage class specifier can appear after other declaration specifiers like the type specifier.

存储类说明符可以出现在其他声明说明符(如类型说明符)之后。

But you shouldn't use it, it's obsolescent, see §6.11.5:

但是你不应该使用它,这是荒废的,看到§6.11.5:

The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

声明中除了声明说明符开头之外的存储类说明符的位置是一个过时的特性。