是否保证在评估恒定表达式时会捕获所有形式的未定义行为
我遇到了以下索赔:
实际上,在评估恒定表达式时,需要捕获语言中的所有形式的UB(尽管不需要捕获标准库中的UB)。
只是运行时UB。
(强调我的)
我的问题是上述陈述在技术上是正确的吗?
在询问用户如何强加此标准后,他们引用了 expr.const#5.8 << /a>,其中指出:
5。表达式E是核心常数表达式,除非遵循抽象机器规则的E评估E([Into.exection])将评估以下一个:
5.8。通过[cpp] into];
在[into]中指定的不确定行为的操作
但是,在阅读了上述[Expr.Const#5.8]之后,我无法弄清楚这意味着在评估恒定表达式时需要捕获所有形式的UB。那么,有人可以澄清一下这引用支持(如果确实如此)中所引用的评论中如何支持?
我还阅读 this 说:
如果行为不确定,编译器可以接受,拒绝它,发出警告,并根据标准,甚至在计算机上崩溃,挂起或安装病毒。
因此,在我看来(阅读第一本注释),在评估恒定表达式和运行时UB期间UB之间存在基本差异。
什么是事实?
I came across the following claim:
Actually, all forms of UB in the language are required to be caught when evaluating a constant expression (though UB in the standard library is not required to be caught). It's only runtime UB where anything can happen.
(emphasis mine)
My question is that is the above statement technically correct?
Upon asking the user how does the standard impose this, they cited expr.const#5.8, which states:
5. An expression E is a core constant expression unless the evaluation of E, following the rules of the abstract machine ([intro.execution]), would evaluate one of the following:
5.8. an operation that would have undefined behavior as specified in [intro] through [cpp];
But after reading the above [expr.const#5.8], I could not figure out how this implies that all forms of UB in the language are required to be caught when evaluating a constant expression. So can someone clarify how does this citation support (if it does) the claim made in the comment quoted above?
I also read this which says:
If the behavior is undefined, the compiler could accept it, reject it, issue a warning, and according to the standard, even crash, hang or install a virus on your computer.
So it seems to me (upon reading the very first comment) that there is a fundamental difference between UB during the evaluation of a constant expression and a runtime UB.
What is the truth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不一定适用于所有形式的UB。根据引用的规则,只有当操作进行评估时,才能通过[cpp] into]; 在[into]中指定的不确定行为。
没有其他部分中指定的其他UB,也不是不是由操作评估引起的UB,可以阻止表达式为核心常数。有一个明确的规则:
此澄清(包括问题上的“句子中指定的句子”)是1952年缺陷报告的解决方案,措辞在C ++ 17中。
为了澄清,该规则会导致某些UB防止表达是核心常数。考虑一个规则需要表达式恒定的情况。这是这样规则的一个例子:
如果某些上下文需要表达式是恒定的,那么表达式不是恒定的将违反该规则。在这种情况下,这种适用:
Not necessarily for all forms of UB. As per the quoted rule, only if operation is that is evaluated would have undefined behavior as specified in [intro] through [cpp];.
No other UB such as specified in other sections, or UB that isn't caused by evaluation of an operation, prevents an expression from being core constant. There is a clarifying rule:
This clarification (including the "as specified in ..." sentence from question) is a resolution to defect report 1952 and the wording is in C++17.
To clarify, the rule causes certain UB to prevent an expression from being core constant. Consider a case where a rule requires an expression to be constant. Here is a an example of such rule:
If some context requires an expression to be constant, then the expression not being constant will violate that rule. In such case this applies: