以 `int typedef my_int;` 形式的 Typedef 声明 [英] Typedef declaration in the form `int typedef my_int;`
问题描述
要将 my_int
声明为 int
的类型别名,我们可以这样写:
To declare my_int
as a type alias for int
we can write:
typedef int my_int; // (1)
奇怪的是,以下似乎也定义了一个 int
别名:
Curiously, the following also seems to define an int
alias:
int typedef my_int; // (2)
我以前从未见过这样的语法.为什么有效?
I have never seen such syntax before. Why does it work?
推荐答案
我阅读 C++ 参考是这样的:(1)和(2)是形式
My reasoning after reading C++ reference is this: (1) and (2) are declarations of the form
specifiers-and-qualifiers declarators-and-initializers;
specifiers-and-qualifiers
是 typedef int
或 int typedef
.
说明符和限定符的顺序无关紧要,(1) 和 (2) 都是类型别名的有效声明.例如,要为 const int
定义别名,我们原则上可以使用以下 6 种组合中的任何一种:
The order of specifiers and qualifiers doesn't matter, and both (1) and (2) are valid declarations of a type alias. For example, to define an alias for const int
we can, in principle, use any of these 6 combinations:
typedef int const my_cint;
typedef const int my_cint;
int typedef const my_cint;
const typedef int my_cint;
int const typedef my_cint;
const int typedef my_cint;
这篇关于以 `int typedef my_int;` 形式的 Typedef 声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!