以每種語言編譯時,在C和C ++中都有效的代碼可否產生不一樣的行爲?

問題:

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.) (「有效」是指具備定義行爲的標準代碼,即不是特定於實現的/未定義的等。) oop

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: 爲了使其成爲合理/有用的比較(我試圖學習一些實用的東西,而不是試圖在問題中發現明顯的漏洞),咱們假設: spa

  • 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
相關文章
相關標籤/搜索