是否缺少必需的包含未定义行为? [英] Is missing a required include undefined behavior?

查看:67
本文介绍了是否缺少必需的包含未定义行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为写出答案时,如何在不包含cmath库的情况下使用pow 我担心已经证明了这一点缺少所需的标头的包含实际上是未定义的行为,但是由于我尚未发现该事实的任何同意,因此我想提出一个正式问题:

As I wrote an answer to How is it possible to use pow without including cmath library I fear to have proven that missing an include of a needed header is actually undefined behavior, but since I have not found any consent of that fact I like to impose the formal question:

缺少必需的标题,即

#include <iostream>

int main()
{
    std::cout << std::pow(10, 2);
}

  1. 格式错误([defns.ill.formed])的代码?
  2. 调用未定义的行为([defns.undefined])?
  3. 如果它不是1和2,是未指定的行为[defns.unspecified]还是实现定义的行为[defns.impl.defined]?
  4. 如果不为1,即该代码格式正确,那会不会与[using.headers]和[intro.compliance]接受并正确执行格式正确的程序"相矛盾?

就像我的 answer 一样,我倾向于肯定这两个问题,但由于未定义行为与格式错误的区别,不需要诊断消息.正如[defns.well.formed]表示构造到ODR的程序格式正确,并且存在规范,例如每当iostream不能定义pow时,有人可能会说这仍然是未指定的行为([defns .unspecified]).对于这样一个重要的问题,我不想仅仅依靠我的标准口译技能来给出明确的答案.请注意,如果代码是UB,也不会问这个问题,那么唯一接受的答案(即唯一的其他答案)不会回答.

As in my answer I tend to affirm both questions, but [using.headers] is very confusing because of Difference between Undefined Behavior and Ill-formed, no diagnostic message required . As [defns.well.formed] implies that a program constructed to the ODR is well formed, and there is specification of whenever the for example iostream must not define pow, one could argue this is still unspecified behavior ( [defns.unspecified]). I don't want to rely only of my standard interpretation skills for a definitive answer for such an important question. Note that the accepted i.e. the only other answer does not answer if the code is UB nor does the question asks it.

推荐答案

该程序是否格式正确或格式错误(具有必需的诊断,因为名称查询不正确,尚不确定)查找pow).可能的说法是,一个C ++标头可能包含另一个标头 ,它授予实施权限 ,以便使该程序只有两种可能的解释.

It is unspecified whether this program is well-formed or ill-formed (with a required diagnostic, because name lookup doesn’t find pow). The possibilities arise from the statement that one C++ header may include another, which grants permission to the implementation to give this program either of just two possible interpretations.

几个相似的规则(例如,一个模板必须至少具有一个有效的潜在专业化条件)(例如)被描述为使程序格式不正确,不需要诊断,但是在这种情况下,自由不会扩展到实现(可以说是更可取的).也就是说,实现 允许以任意的方式处理格式不正确的程序,只要它发出至少一个诊断消息即可,因此对它进行分组并不是完全不合理的即使症状在实践中有所不同,这种情况仍具有真正的不确定行为.

Several similar rules (e.g., that a template must have at least one valid potential specialization) are described as rendering the program ill-formed, no diagnostic required, but in this situation that freedom is not extended to the implementation (which is arguably preferable). That said, an implementation is allowed to process an ill-formed program in an arbitrary fashion so long as it issues at least one diagnostic message, so it’s not completely unreasonable to group this situation with true undefined behavior even though the symptoms differ usefully in practice.

这篇关于是否缺少必需的包含未定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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