c++ - Typedef 声明的形式为 `int typedef my_int;`

标签 c++ typedef

要将 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 intint 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/

相关文章:

c++ - 使用 sin() 生成的声音中的金属声音

c++ - Makefile 在 Linux 下工作但在 Windows 下不工作,无法在子目录中找到文件

c++ - NumberFormat/DecimalFormat 将某些浮点值视为 long 而不是 double

c - 结构的 typedef 似乎没有在头文件中通过?

c - 为什么使用 typedef *after* 结构定义?

c - 如果 typedef 是存储类,则 typedef 的范围和生命周期是多少?

C++:由于模板类中的 typedef 返回类型,在错误之前奇怪地缺少 ';'

c++ - std::unique_ptr 转移 const 对象的所有权

c++ - 右值参数无法在函数重载中解析

c++ - 从 ‘const BYTE* {aka const unsigned char*}’ 到 ‘BYTE_PTR {aka unsigned char*}’ 的无效转换