在 int32_t 上调用析构函数是否合法?

Is it legal to call a destructor on int32_t?

我刚刚发现以下代码不是有效的 C++(它在 ~ 之后的 int 处不解析):

int x = 5;
x.~int();

但是,以下代码片段确实有效:

int32_t x = 5;
x.~int32_t();

这是因为 int32_t 在我的特定 C++ 实现中是一个 typedef,并且析构函数显然可以在任何类型定义的类型上调用。

我的问题是:是否需要任何 C++ 实现才能允许第二个片段进行编译?特别是,int32_t 是否保证是 typedef,如果编译器知道 typedef typedef 将某些内容转换为 int,是否需要编译器允许销毁 typedef?

明确要求 int32_t 是 typedef。我们从 [cstdint.syn]/2:

开始

The header defines all functions, types, and macros the same as 7.18 in the C standard.

因此我们从那里查看对 C 库的要求:

The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two’s complement representation.

[强调]

所以是的,int32_t 必须是 "typedef name"。

尽管(据我所知)它从未在规范文本中直接说明,但以下说明清楚地表明,为解析为内置类型的 typedef 调用析构函数旨在编译并成功( [ class.dtor]/16):

Note: the notation for explicit call of a destructor can be used for any scalar type name (5.2.4). Allowing this makes it possible to write code without having to know if a destructor exists for a given type. For example,

typedef int I;
I* p;
p->I::~I();