sizeof operator returns 1bytes for VOID operator

sizeof operator returns 1bytes for VOID operator

我正在查看 C++ 中的 sizeof 运算符。我遇到了意想不到的语义错误。我读过 void 数据类型没有内存大小。但是,在我的程序中,sizeof operator returns 1 用于 void 数据类型。这怎么可能?

#include<iostream>
int main()
{
     std::cout<<"Void: "<<sizeof(void)<<" bytes\n";
     return 0;
}

我正在使用 CodeBlocks 编写代码,我的 OS 是 Windows 10 x64。

这是一个GCC extension:

In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.

A consequence of this is that sizeof is also allowed on void and on function types, and returns 1.

The option -Wpointer-arith requests a warning if these extensions are used.