是"typedef"吗?在类型和别名之间是否符合标准? [英] Is "typedef" in between the type and the alias standard-conformant?

查看:91
本文介绍了是"typedef"吗?在类型和别名之间是否符合标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一些代码,其中typedef关键字位于类型和别名之间,如in

I stumbled upon some code where the typedef keyword is in between the type and the alias name like in

int typedef INT;

它以gcc和clang进行编译(实时示例).我对标准语言的理解不是很好.所以我的问题是:这个标准符合吗?我可以依靠编译器来支持它吗?

It compiles in gcc and clang (live example). I am not very good in understanding standardese. So my question is: Is this standard conformant? Can I rely on compilers to support it?

推荐答案

TL/DR版本

是,int typedef INT符合要求.

James Michener版本

C声明语法( C 2011在线草案):

6.7声明

语法

1   declaration:
        declaration-specifiers init-declarator-listopt ;
        static_assert-declaration

    declaration-specifiers:
        storage-class-specifier declaration-specifiersopt
        type-specifier declaration-specifiersopt
        type-qualifier declaration-specifiersopt
        function-specifier declaration-specifiersopt
        alignment-specifier declaration-specifiersopt
...
6.7 Declarations

Syntax

1    declaration:
        declaration-specifiers init-declarator-listopt ;
        static_assert-declaration

    declaration-specifiers:
        storage-class-specifier declaration-specifiersopt
        type-specifier declaration-specifiersopt
        type-qualifier declaration-specifiersopt
        function-specifier declaration-specifiersopt
        alignment-specifier declaration-specifiersopt
...

这是说在一个声明中,您可以具有一个或多个声明说明符的序列,其中每个声明说明符可以是一个存储类说明符(autoexternstatictypedef),类型说明符(intfloatchar等),类型限定符(constrestrictvolatile等),函数说明符(inline)或对齐说明符.

What this says is that in a single declaration you can have a sequence of one or more declaration specifiers, where each declaration specifier can be a storage class specifier (auto, extern, static, typedef), a type specifier (int, float, char, etc.), a type qualifier (const, restrict, volatile, etc.), a function specifier (inline), or an alignment specifier.

各种说明符出现的顺序无关紧要; static const short int x;可以写为int static short const xint short const static x等.作为实践,大多数人都将存储类说明符放在第一位,然后是任何函数或对齐说明符(如果需要),然后是任何类型限定符,然后是类型说明符,因此>大多数人们将如何编写该声明.

The order in which various specifiers appear doesn't matter; static const short int x; may be written as int static short const x, or int short const static x, etc. As a matter of practice, most people put the storage class specifier first, then any function or alignment specifiers (if necessary), then any type qualifiers, then type specifiers, so static const short int x is how most people would write that declaration.

这种语法使我们可以编写诸如long doublelong longunsigned long int等的类型.

This syntax is what allows us to write types like long double or long long or unsigned long int, etc.

请注意,虽然语法允许类型说明符的任意(任意长)序列,但有一个语义规则仅允许相对少数.例如,您不能编写short short short short xlong long long double y.仅允许以下序列:

Note that while the syntax allows arbitrary (and arbitrarily long) sequences of type specifiers, there's a semantic rule that only allows a relative few. You can't write short short short short x, for example, or long long long double y. Only the following sequences are allowed:

约束

2   每个声明中的声明说明符中至少应提供一个类型说明符, 并在每个结构声明和类型名称的specifier-qualifier列表中.每个清单 类型说明符应为以下多个集合之一(如果存在,则用逗号分隔) 每个项目多于一个多集);类型说明符可能以任何顺序出现 与其他声明说明符混合.
       -void
       -char
       -signed char
       -unsigned char
       -short, signed short, short int, or signed short int
       -unsigned short, or unsigned short int
       -int, signed, or signed int
       -unsigned, or unsigned int
       -long, signed long, long int, or signed long int
       -unsigned long, or unsigned long int
       -long long, signed long long, long long int, or
            signed long long int
       -unsigned long long, or unsigned long long int
       -float
       -double
       -long double
       -_Bool
       -float _Complex
       -double _Complex
       -long double _Complex
       -原子类型说明符
       -结构或联合说明符
                       -typedef名称
Constraints

2    At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name. Each list of type specifiers shall be one of the following multisets (delimited by commas, when there is more than one multiset per item); 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, or signed int
        — unsigned, or unsigned int
        — long, signed long, long int, or signed long int
        — unsigned long, or unsigned long int
        — long long, signed long long, long long int, or
            signed long long int
        — unsigned long long, or unsigned long long int
        — float
        — double
        — long double
        — _Bool
        — float _Complex
        — double _Complex
        — long double _Complex
        — atomic type specifier
        — struct or union specifier
        — enum specifier
        — typedef name

附录

该语言的未来版本可能会将存储类说明符限制为声明的开头,因此int typedef INT在未来的编译器中可能不合法.

As Keith points out in the comment below, a future revision of the language may limit storage class specifiers to the beginning of the declaration, so int typedef INT may not be legal under a future compiler.

这篇关于是"typedef"吗?在类型和别名之间是否符合标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆