ISO C 中数组的左值到右值转换
C++ ANSI ISO IEC 14882 2003 附录 C.1(第 668 页):
更改:条件表达式、赋值表达式或逗号表达式的结果可能是 Bean 左值
理由:C++是面向对象的语言,相对更注重左值。例如,函数可能返回左值。
对原始特征的影响:更改明确定义的特征的语义。某些隐式依赖左值到右值转换的 C 表达式将产生不同的结果。例如,
char arr[100];
sizeof(0, arr)
在 C++ 中生成 100,在 C 中生成 sizeof(char*)
。
...
我今天刚读到这篇文章,我记得几个月前我的一个朋友提出了一个问题,即编写一个函数,如果用 C++ 编译,则返回 0,如果用 C 编译,则返回 1我利用 C 中结构位于外部作用域的事实解决了这个问题。因此,考虑到这个新信息,我决定这将是上述问题的另一种解决方案,我在 Microsoft Visual Studio 2008 上尝试过,但无论它是编译为 C 还是 C++ 代码 sizeof(0, arr)
总是产生 4。所以有 2 个问题:
1.什么是 ISO C?这是当前的C标准吗?是唯一的吗(听说C正在快速发展) 2. 这是微软C++的bug吗?
TIA
编辑:抱歉与输出混淆并对其进行了编辑:
C++ ANSI ISO IEC 14882 2003 Annex C.1 (page 668):
Change: The result of a conditional expression, an assignment expression, or a comma expression may bean lvalue
Rationale: C + + is an object-oriented language, placing relatively more emphasis on lvalues. For example, functions may return lvalues.
Effect on original feature: Change to semantics of well-defined feature. Some C expressions that implicitly rely on lvalue-to-rvalue conversions will yield different results. For example,
char arr[100];
sizeof(0, arr)
yields 100 in C + + and sizeof(char*)
in C.
...
I was reading this just today and I remembered that a couple of months a go a friend of mine proposed a problem wchich was to write a function that would return 0 if it were compiled with C++ and 1 if it were compiled with C. I solved it taking advantage of the fact that in C a struct was in the outer scope. So, considering this new information, I decided that this would be another solution to the above problem, which I tried on Microsoft Visual Studio 2008, but regardless of whether it is compiled as C or C++ code sizeof(0, arr)
always yields 4. So 2 questions:
1.What is ISO C? Is it the current C standard? Is it the only one (I hear C is rapidly evolving)
2. Is this a microsoft C++ bug?
TIA
Edit: Sorry got mixed up with the output and edited it:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ISO C 是 C 标准。目前的版本是 C99,但 C1x 即将推出。如果所说的快速是指每十年左右就有一个新标准,那么是的,它正在迅速发展:-)
ISO C99 第 6.5.3.4/3 节指出:
ISO C is the C standard. The current one is C99 but C1x is right around the corner. If by rapid, you mean a new standard every decade or so, then yes, it is rapidly evolving :-)
Section 6.5.3.4/3 of ISO C99 states:
Microsoft Visual C 仍然[仅]支持 C89,而 gcc/clang 等其他编译器也支持 C99是现行标准。
C99 [第
6.5.17/2
节] 说因此
sizeof (0,arr)
的结果将是sizeof(char*)
[由于隐式左值
到右值
转换/自动衰减为指针类型]不是100*sizeof(char)
sizeof(arr) 会给出来自
6.5.3.4/3
的100*sizeof(char)
95) 逗号运算符不会产生左值。
C++03 [
5.18/1
] 逗号运算符所以
sizeof(0, arr) = sizeof (arr)
并且等于100* sizeof(char)
而不是 =sizeof(char*)< /代码>。
因此 MSVC++ 给出了错误的结果(对于 C++ 代码)。
Microsoft Visual C still supports C89 [only] whereas other compilers like gcc/clang etc support C99 too which is the current Standard.
C99 [Section
6.5.17/2
] saysThus the result of
sizeof (0,arr)
would besizeof(char*)
[due to the implicitlvalue
torvalue
conversion /automatic decay to pointer type] not100*sizeof(char)
sizeof(arr)
would have given100*sizeof(char)
from6.5.3.4/3
95) A comma operator does not yield an lvalue.
C++03 [
5.18/1
] Comma OperatorSo
sizeof(0, arr) = sizeof (arr)
and which would be equal to100* sizeof(char)
and not =sizeof(char*)
.So MSVC++ is giving incorrect result (in case of C++ code).
对于数组,
sizeof
返回总大小。请小心作为指针传递的数组。C99标准:
For arrays, the
sizeof
returns the total size. Be careful about arrays passed as pointers.C99 standard: