简体   繁体   English

sizeof(void())是一个合法的表达式吗?

[英]Is sizeof(void()) a legal expression?

From [5.3.3/1] , I found that: [5.3.3 / 1]开始 ,我发现:

The sizeof operator shall not be applied to an expression that has function or incomplete type sizeof运算符不应用于具有函数或不完整类型的表达式

From [3.9/5] I found that: [3.9 / 5]我发现:

Incompletely-defined object types and cv void are incomplete types 不完全定义的对象类型和cv void是不完整的类型

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). 无论如何,对于sizeof不评估它的操作数,我会说sizeof(void())是一个合法的表达式(实际上GCC编译它,结果是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的类型,也没有提到具有实现定义大小的类型的列表。

The question is thus: is sizeof(void()) a legal expression? 问题是: sizeof(void())是一个合法的表达方式吗?
Is it guaranteed to have size equal to 1? 是否保证大小等于1?
Or is it a legal expression resulting in an UB and that's all? 或者这是一个导致UB的法律表达,这就是全部?

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

From looking at CppReference.com - sizeof operator , the documentation literally states: 从查看CppReference.com - sizeof运算符 ,文档字面上指出:

sizeof cannot be used with function types , incomplete types, or bit-field glvalues. sizeof不能与函数类型 ,不完整类型或位字段glvalues一起使用。

And since void() is a function type, then sizeof(void()) is not a legal expression. 由于void()是一个函数类型,因此sizeof(void())不是一个合法的表达式。

In their usage example, we can see their error comment on this line: 在他们的使用示例中,我们可以在此行中看到他们的错误评论:

std::cout << "size of function: " << sizeof(void()) << '\n'; // error

Also, if you compile the code, such as the example below: 此外,如果您编译代码,例如下面的示例:

#include <iostream>

int main()
{
   std::cout << sizeof(void());
}

The code compiles correctly and produces a value of 1, but if you look at the compilation, you see this: 代码正确编译并生成值1,但如果查看编译,您会看到:

main.cpp: In function 'int main()': main.cpp:在函数'int main()'中:

main.cpp:5:29: warning: invalid application of 'sizeof' to a function type [-Wpointer-arith] main.cpp:5:29:警告:'sizeof'无效应用于函数类型[-Wpointer-arith]

std::cout << sizeof(void()); std :: cout << sizeof(void());

So, it is evident that sizeof() doesn't apply for function types, so the code produces a warning. 因此,很明显sizeof()不适用于函数类型,因此代码会产生警告。 It is invalid. 它无效。


Code here 代码在这里


A little premise. 一个小前提。

The question arose from a misinterpretation of the sizeof operator. 这个问题产生于对运营商sizeof的误解。
In fact the OP considered void() an expression that has incomplete type in the context of sizeof and the question itself can be read as - why sizeof accept the expression void() , that is an incomplete type and should not be accepted as mentioned in the working draft ? 事实上,OP认为void()是一个在sizeof上下文中具有不完整类型的表达式,并且问题本身可以理解为 - 为什么sizeof接受表达式void() ,这是一个不完整的类型,不应该被接受,如工作草案
That's why [3.9/5] is mentioned actually, otherwise it wouldn't have made sense. 这就是实际提到[3.9 / 5]的原因,否则它就没有意义了。

That said, the fact is that the question contains actually two interesting questions: 也就是说,事实是这个问题实际上包含两个有趣的问题:

  • Why is sizeof(void()) not legal? 为什么sizeof(void())不合法?
    This is the actual question as from the title itself. 这是标题本身的实际问题。

  • Why is sizeof((void())) not legal? 为什么sizeof((void()))不合法?
    This is the intended question of the OP. 这是OP的预期问题。

Answers below: 答案如下:

  • void() in sizeof(void()) is interpreted as a function type and it is ill-formed as for [5.3.3/1] (emphasis mine): void()sizeof(void())被解释为一个功能类型,它是形成不良的,作为[5.3.3 / 1] (重点煤矿):

    The sizeof operator shall not be applied to an expression that has function or incomplete type, to the parenthesized name of such types , [...] sizeof运算符不应该应用于具有函数或不完整类型的表达式,这些类型的括号名称 ,[...]

  • (void()) in sizeof((void())) is an expression that has incomplete type void (note that sizeof is an unevaluated context) and it is ill-formed as for [5.3.3/1] (emphasis mine): (void())sizeof((void()))是具有不完整的类型的表达式void (注意sizeof是一个未计算的上下文中),它是形成不良的,作为[5.3.3 / 1] (重点煤矿) :

    The sizeof operator shall not be applied to an expression that has function or incomplete type , to the parenthesized name of such types, [...] sizeof运算符不应该应用于具有函数或不完整类型的表达式,这些类型的括号名称,[...]

In both cases GCC compiles the code with a warning. 在这两种情况下,GCC都会使用警告编译代码。

As already highlighted in the docs here http://en.cppreference.com/w/cpp/language/sizeof 正如这里的文档中已经强调的那样http://en.cppreference.com/w/cpp/language/sizeof

Notes 笔记

sizeof() cannot be used with function types , incomplete types, or bit-field glvalues. sizeof()不能与函数类型 ,不完整类型或位字段glvalues一起使用。

Since void() is a function type, so its not a valid type of sizeof() 由于void()是函数类型,因此它不是sizeof()的有效类型

Note: 注意:

void() is a function that takes no arguments and returns nothing void()是一个不带参数的函数,不返回任何内容

Quoting a Example from Docs: 从文档引用示例:

//<< "size of function: " << sizeof(void()) << '\n'  // error

So Answers to your questions: 所以回答你的问题:

1)No it is not a legal Expression. 1)不,这不是一个法律表达。

2)It will show as 1 , but will show a warning 2)它将显示为1,但会显示警告

3)same as 1). 3)与1)相同。

Straigth from the C99 reference NO 来自C99参考号的Straigth

Stated in the document under section 6.5.3.4 The sizeof operator : 6.5.3.4节中的文件中陈述了sizeof运算符

The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member. sizeof运算符不应该应用于具有函数类型或不完整类型的表达式,这种类型的带括号的名称,或者应用于指定位字段成员的表达式。

According to item 19 and 20 of section 6.2.5 Types : 根据6.2.5节的第19和20项:

  1. The void type comprises an empty set of values; void类型包含一组空值; it is an incomplete type that cannot be completed. 它是一种不完整的类型 ,无法完成。
  2. ...A function type describes a function with specified return type. ...函数类型描述具有指定返回类型的函数。 A function type is characterized by its return type and the number and types of its parameters. 函数类型的特征在于其返回类型以及其参数的数量和类型。 A function type is said to be derived from its return type, and if its return type is T, the function type is sometimes called ''function returning T''. 函数类型据说是从它的返回类型派生的,如果它的返回类型是T,函数类型有时被称为''函数返回T''。 The construction of a function type from a return type is called ''function type derivation''. 从返回类型构造函数类型称为“函数类型派生”。

Thus void() is a function type derived from an incomplete type and its illegal as operand for the sizeof. 因此,void()是一个派生自不完整类型的函数类型,它的非法作为sizeof的操作数。 That said its returns will depends on the compiler implementation and does not have any return value guaranteed 这表示它的返回将取决于编译器的实现,并没有保证任何返回值

C99 Standard C99标准

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM