扩展std命名空间的原因是什么被认为是未定义的行为? [英] What are the reasons that extending the std namespace is considered undefined behavior?

查看:215
本文介绍了扩展std命名空间的原因是什么被认为是未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要向 std 命名空间添加名称未定义的行为?

Why is adding names to the std namespace undefined behaviour?

明显的答案是说,例如in C ++ 14 [namespace.std] 17.6.4.2.1 / 1:

The obvious answer is "because the standard says so," e.g. in C++14 [namespace.std] 17.6.4.2.1/1:


如果C ++程序的行为是未定义的除非另有规定,否则将命名空间 std 或命名空间 std 中的
命名空间添加声明或定义。 ...

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. ...

但是,我真的对这个裁决的原因感兴趣。我当然可以理解添加已经在 std 中的名称的重载可能会破坏行为;

However, I would be really interested in the reasons for this ruling. I can of course understand adding overloads of names already in std could break behaviour; but why is adding new, unrelated names a problem?

程序可能已经在宏中破坏了 std 是为什么几乎所有的标准库实现必须只包含所有非公共部分的保留名称(双下划线和起始 - 下划线,后跟资本)。

Programs can already wreak havoc inside std with macros, which is why pretty much all standard library implementations have to consist solely of reserved names (double-underscore and starting-underscore-followed-by-capital) for all non-public parts.

我真的会感兴趣的情况下,这样的东西可能有问题:

I would really be interested in a situation in which something like this can be problematic:

namespace std
{
  int foo(int i)
  { return i * 42; }
}

#include <algorithm>  // or one or more other standard library headers

应付:

#define foo %%

#include <algorithm>  // or one or more other standard library headers

此未定义行为的原理是什么? p>

What is the rationale for this Undefined Behaviour?

推荐答案

这里有几个原因:


  1. 即使必须将标题中的名称标记为避免与宏进行交互,在实际实施代码的源文件中,对于名称不存在此要求。如果一个实现使用 :: std :: foo(int)作为其实现的一部分,将违反一个定义规则。

  2. 标准预计将增长。如果名称可以添加到命名空间 std 中,则添加到标准C ++库中的任何名称都可能是突变。在某种程度上,这已经是真的,在任何这样的名称可以是一个宏,但它被认为可以打破这些。

  3. 实际上没有必要添加命名空间到命名空间 std :它们可以被添加到任意其他命名空间,即,即使上面给出的动机不是特别强,该限制也不被认为是任何形式的事物。 ...并且如果影响行为的原因是为名称空间 std 添加名称

  1. Even if names in headers have to be uglified to avoid interactions with macros, this requirement does not exist for name in the source files actually implementing the code. If an implementation does use ::std::foo(int) as part of its implementation it would be a violation of the one definition rule.
  2. The standard is expected to grow. If names could be added to namespace std any name added to the standard C++ library would be a likely breaking change. To some extend this is already true in the sense that any such name could be a macro but it is considered acceptable to break those.
  3. There is actually no need to add namespace to namespace std: they can be added to arbitrary other namespace, i.e., even if the motivations given above are not particular strong, the restriction isn't considered to be matter in any form. ... and if there is a reason to add a name to namespace std it clearly does affect the behavior.

这篇关于扩展std命名空间的原因是什么被认为是未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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