要将 my_int
声明为 int
的类型别名,我们可以这样写:
typedef int my_int; // (1)
奇怪的是,以下内容似乎也定义了 int
别名:
int typedef my_int; // (2)
我以前从未见过这样的语法。为什么它有效?
最佳答案
读完我的推理C++ reference是这样的:(1) 和 (2) 是以下形式的声明
specifiers-and-qualifiers declarators-and-initializers;
其中说明符和限定符
为typedef int
或int typedef
。
说明符和限定符的顺序并不重要,(1) 和 (2) 都是类型别名的有效声明。例如,要为 const int
定义别名,原则上我们可以使用以下 6 种组合中的任何一种:
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;
关于c++ - Typedef 声明的形式为 `int typedef my_int;`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58781929/