“未定义的行为”扩展到编译时? [英] Does "undefined behaviour" extend to compile-time?

查看:157
本文介绍了“未定义的行为”扩展到编译时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都听说过警告,如果您在C或C ++中调用未定义的行为,则任何东西都可能发生。



这是否限制为任何运行时行为,还是包括任何编译时行为?特别是,编译器在遇到调用未定义行为的构造时,允许拒绝代码(在标准中没有其他要求这样做),甚至崩溃?

解决方案


您已经忽略了实际的定义并专注于注释,标准强加 / em>。 - @ R.MartinhoFernandes


上述讯息由指定使用者在 Lounge< C ++> ,并提出非常有效的论据;




>

! ! !



undefined-behavior 甚至延伸到编译器解析输入数据(即代码)的远角,



要回答您的问题,请使用 C ++ 11 C99 一个句子;




  • 未定义的行为不限于运行时执行,



    以文档化的方式表现环境是一种奇怪的语句,你几乎可以编写一个编译器



    sup> 1。引自C ++ 11 / C99标准






    c ++ 11 < h3>


    1.3.24 [defns.undefined]



    em>;本国际标准
    不强制规定的行为



    [注意


    当本国际标准省略
    行为的任何明确定义或程序使用错误的构造或错误的
    数据时,未定义的行为可能是预期的
    。 / p>

    允许的未定义行为范围从完全忽略具有不可预测结果的情况在翻译期间行为

    环境(有或没有发出诊断消息)特有的文件化方式程序执行,到
    终止翻译或执行 (发出
    诊断消息)。



    许多错误的程序结构不会产生未定义的行为; b $ b






    c99




    3.4.3 - 未定义行为




    1. 行为,使用不可移植或错误的程序结构或错误数据,


    2. 注意可能未定义的行为范围从完全忽略具有不可预测结果的情况
      环境(带有或不带有诊断消息)特征的文档化方式,
      或程序执行
      终止翻译或执行(发出
      诊断消息)。




    We've all heard the warnings that if you invoke undefined behaviour in C or C++, anything at all can happen.

    Is this limited to any runtime behaviour at all, or does this also include any compile-time behaviour? In particular, is a compiler, upon encountering a construct that invokes undefined behaviour, allowed to reject the code (in the absence of other requirements in the standard to do so), or even to crash?

    解决方案

    "You're all ignoring the actual definition and focusing on the note, The standard imposes no requirements." - @R.MartinhoFernandes

    The message above was written by the given user in Lounge<C++> and makes a very valid argument; the standard doesn't impose any requirements when it comes to code that invokes undefined behavior.


    ! ! !

    undefined-behavior stretches even to the far corner of parsing the input data (ie. code) by the compiler, as verified with the below quotations from both the C++11 and C99 standards.

    To answer your question with one sentence;

    • undefined behavior is not limited to runtime execution, and it's permissible to crash during compilation "in a documented manner characteristic of the environment" 1

    "in a documented manner characteristic of the environment" is a kind of odd statement, you could pretty much write a compiler documenting that it might crash upon any given code (that's invalid) to grant it the possibility to crash whenever it wants to.

    1. quote from the C++11/C99 standards


    1.3.24 [defns.undefined]

    Undefined behavior; behavior for which this International Standard imposes no requirements

    [ Note:

    Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data.

    Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

    Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed.

    end note ]


    3.4.3 - Undefined Behavior

    1. behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this >International Standard imposes no requirements

    2. NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

    这篇关于“未定义的行为”扩展到编译时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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