以每种语言编译时,在C和C ++中都有效的代码能否产生不同的行为?

2021-01-29 08:14

阅读:636

问题:

C and C++ have many differences, and not all valid C code is valid C++ code. C和C ++有很多区别,并非所有有效的C代码都是有效的C ++代码。
(By "valid" I mean standard code with defined behavior, ie not implementation-specific/undefined/etc.) (“有效”是指具有定义行为的标准代码,即不是特定于实现的/未定义的等。)

Is there any scenario in which a piece of code valid in both C and C++ would produce different behavior when compiled with a standard compiler in each language? 在任何情况下,使用每种语言的标准编译器进行编译时,在C和C ++中都有效的一段代码会产生不同的行为吗?

To make it a reasonable/useful comparison (I‘m trying to learn something practically useful, not to try to find obvious loopholes in the question), let‘s assume: 为了使其成为合理/有用的比较(我试图学习一些实用的东西,而不是试图在问题中发现明显的漏洞),我们假设:

  • Nothing preprocessor-related (which means no hacks with #ifdef __cplusplus , pragmas, etc.) 与预处理器无关(这意味着#ifdef __cplusplus , #ifdef __cplusplus不会引起黑客攻击)
  • Anything implementation-defined is the same in both languages (eg numeric limits, etc.) 实现定义的任何内容在两种语言中都是相同的(例如,数值限制等)
  • We‘re comparing reasonably recent versions of each standard (eg say, C++98 and C90 or later) 我们正在比较每个标准的合理最新版本(例如C ++ 98和C90或更高版本)
    If the versions matter, then please mention which versions of each produce different behavior. 如果版本很重要,请说明每个版本产生不同的行为。

解决方案:

参考一: https://stackoom.com/question/s4g8/以每种语言编译时-在C和C-中都有效的代码能否产生不同的行为
参考二: https://oldbug.net/q/s4g8/Can-code-that-is-valid-in-both-C-and-C-produce-different-behavior-when-compiled-in-each-language


评论


亲,登录后才可以留言!