sizeof 接受两个参数 [英] sizeof taking two arguments

查看:27
本文介绍了sizeof 接受两个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ IS (2003. 它也在 C++11 IS) 的 C.1.3 中,该标准指出了 ISO C 和 C++ 之间的区别;即对于

In C.1.3 of the C++ IS (2003. It's in the C++11 IS, too), the standard points out a difference between ISO C and C++; namely, for

char arr[100];

sizeof(0, arr) 在 C 中返回 sizeof(char*),但在 C++ 中返回 100.

sizeof(0, arr) returns sizeof(char*) in C, but 100 in C++.

我找不到关于带有两个参数的 sizeof 的文档.明显的回退是逗号运算符,但我不这么认为:sizeof(arr) 在 C 中是 100;sizeof(0, arr)sizeof(char*).sizeof(0, arr)sizeof(arr) 在 C++ 中都是 100.

I can find no documentation for sizeof taking two arguments. The obvious fallback is the comma operator, but I don't think so: sizeof(arr) in C is 100; sizeof(0, arr) is sizeof(char*). Both sizeof(0, arr) and sizeof(arr) are 100 in C++.

在这种情况下,我可能遗漏了 IS 的全部内容.任何人都可以帮忙吗?这类似于 09 年讨论的一个问题,但没有人提到 IS,我认为没有给出正确答案.

I may be missing the whole point of the IS in this context. Can anyone help? This is similar to a question discussed back in '09, but no one referred to the IS, and I don't think the correct answer was given.

编辑:实际上,IS 正在谈论逗号运算符.因此,出于某种原因,(0, arr) 在 C 中返回一个 char*,而在 C++ 中返回一个 char[100].为什么?

Edit: Actually, the IS is talking about the comma operator. So, for some reason (0, arr) returns a char* in C, but a char[100] in C++. Why?

推荐答案

在 C 中,由于与右值和左值相关的逗号运算符的不同规范(不是唯一的地方,如可以发现差异).在 C++ 中,数组仍然是一个数组,产生正确的结果.

In C then the array is decaying to a pointer, because of the different specification of the comma operator with relation to rvalues and lvalues (not the only place such a difference can be found). In C++ then the array stays an array, yielding the correct result.

这篇关于sizeof 接受两个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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