是sizeof(void())一个合法的表达式吗? [英] Is sizeof(void()) a legal expression?

查看:225
本文介绍了是sizeof(void())一个合法的表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[5.3.3 / 1] ,我发现:


sizeof运算符不适用于具有函数或不完整类型的表达式

The sizeof operator shall not be applied to an expression that has function or incomplete type

[3.9 / 5] ,我发现:


未完全定义的对象类型和cv void是不完整的类型

Incompletely-defined object types and cv void are incomplete types

无论如何,对于 sizeof 不评估它的操作数,我会说, sizeof(void())是一个法律表达式(实际上是GCC编译它,结果是1)。

另一方面,从在讨论 sizeof 时没有提到 void ,当提及大小为1的类型时,

Anyway, for sizeof does not evaluate it's operands, I would have said that sizeof(void()) was a legal expression (actually GCC compiles it and the result is 1).
On the other side, from here, void is not mentioned while discussing sizeof, neither when the types having size 1 are mentioned, nor in the list of the ones having an implementation defined size.

问题是: sizeof(void) ())一个法律表达式?

它保证有大小等于1?

或者是一个导致UB的法律表达式,所有?

The question is thus: is sizeof(void()) a legal expression?
Is it guaranteed to have size equal to 1?
Or is it a legal expression resulting in an UB and that's all?

推荐答案

void()是一个函数类型函数不需要参数并且不返回任何内容),因此它不是 sizeof()中的有效类型。

void() is a function type (it's a function which takes no arguments and returns nothing), so it's not a valid type in sizeof().

这篇关于是sizeof(void())一个合法的表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆