我相信 [dcl.typedef]/1 有缺陷,否则我错过了什么?
I believe [dcl.typedef]/1 has a defect, otherwise what am I missing?
If the decl-specifier-seq contains the typedef
specifier, the
declaration is called a typedef declaration and each declarator-id
is declared to be a typedef-name, synonymous with its associated
type ([dcl.typedef]).
[Note 4: Such a declarator-id is an identifier ([class.conv.fct]).
— end note]
If the decl-specifier-seq contains no typedef
specifier, the
declaration is called a function declaration if the type associated
with a declarator-id is a function type ([dcl.fct]) and an object
declaration otherwise.
Declarations containing the decl-specifier typedef
declare
identifiers that can be used later for naming fundamental or compound
types. The typedef
specifier shall not be combined in a
decl-specifier-seq with any other kind of specifier except a defining-type-specifier, and it shall not be used in the decl-specifier-seq of a parameter-declaration ([dcl.fct]) nor in the decl-specifier-seq of a function-definition ([dcl.fct.def]).
If a typedef
specifier appears in a declaration without a
declarator, the program is ill-formed.
下面的代码显示了两个 typedef 声明:
typedef struct A{ int i; } structA;
和
typedef const int CI;
第一个就可以了,满足[dcl.typedef]/1中高亮的文字,如下图:
struct A { int; }
是 class-specifier which is a defining-type-specifier.
structA
是 typedef-name.
众所周知,第二个typedef
声明编译,但不应该,同样根据上面突出显示的文本,如下所示:
const
是一个 cv-qualifier,它是一个 type-specifier, which is a defining-type-specifier.
int
是一个 简单类型说明符 ,它是一个 type-specifier, which is a defining-type-specifier.
也就是说,我们在同一个typedef
声明中有两个defining_type_specifiers。我的母语不是英语,我假设文章 'a' 中的表达式“except a defining-type-specifier”表示只有一个 [=根据提到的段落,55=]defining_type_specifier 在 typedef
声明中被接受。
Code:
typedef struct A{ int i; } structA;
typedef const int CI;
int main()
{
}
下面描述的这条规则有例外情况,允许您编写这样的结构:
typedef const signed short int si;
As a general rule, at most one defining-type-specifier
is allowed in the complete decl-specifier-seq
of a declaration or in a defining-type-specifier-seq
, and at most one type-specifier
is allowed in a type-specifier-seq
. The only exceptions to this rule are the following:
const
can be combined with any type specifier except itself.
volatile
can be combined with any type specifier except itself.
signed
or unsigned
can be combined with char
, long
, short
, or int
.
short
or long
can be combined with int
.
long
can be combined with double
.
long
can be combined with long
.
If the decl-specifier-seq contains the
typedef
specifier, the declaration is called a typedef declaration and each declarator-id is declared to be a typedef-name, synonymous with its associated type ([dcl.typedef]).[Note 4: Such a declarator-id is an identifier ([class.conv.fct]). — end note]
If the decl-specifier-seq contains no
typedef
specifier, the declaration is called a function declaration if the type associated with a declarator-id is a function type ([dcl.fct]) and an object declaration otherwise.
Declarations containing the decl-specifier
typedef
declare identifiers that can be used later for naming fundamental or compound types. Thetypedef
specifier shall not be combined in a decl-specifier-seq with any other kind of specifier except a defining-type-specifier, and it shall not be used in the decl-specifier-seq of a parameter-declaration ([dcl.fct]) nor in the decl-specifier-seq of a function-definition ([dcl.fct.def]). If atypedef
specifier appears in a declaration without a declarator, the program is ill-formed.
下面的代码显示了两个 typedef 声明:
typedef struct A{ int i; } structA;
和
typedef const int CI;
第一个就可以了,满足[dcl.typedef]/1中高亮的文字,如下图:
struct A { int; }
是 class-specifier which is a defining-type-specifier.
structA
是 typedef-name.
众所周知,第二个typedef
声明编译,但不应该,同样根据上面突出显示的文本,如下所示:
const
是一个 cv-qualifier,它是一个 type-specifier, which is a defining-type-specifier.
int
是一个 简单类型说明符 ,它是一个 type-specifier, which is a defining-type-specifier.
也就是说,我们在同一个typedef
声明中有两个defining_type_specifiers。我的母语不是英语,我假设文章 'a' 中的表达式“except a defining-type-specifier”表示只有一个 [=根据提到的段落,55=]defining_type_specifier 在 typedef
声明中被接受。
Code:
typedef struct A{ int i; } structA;
typedef const int CI;
int main()
{
}
下面描述的这条规则有例外情况,允许您编写这样的结构:
typedef const signed short int si;
As a general rule, at most one
defining-type-specifier
is allowed in the completedecl-specifier-seq
of a declaration or in adefining-type-specifier-seq
, and at most onetype-specifier
is allowed in atype-specifier-seq
. The only exceptions to this rule are the following:
const
can be combined with any type specifier except itself.volatile
can be combined with any type specifier except itself.signed
orunsigned
can be combined withchar
,long
,short
, orint
.short
orlong
can be combined withint
.long
can be combined withdouble
.long
can be combined withlong
.