C++编译器实现命名空间

发布于 2024-11-15 04:02:13 字数 78 浏览 3 评论 0原文

从C++编译器的角度来看,命名空间只是一个名称装饰约定吗?我检查了生成的程序集列表,发现一切看起来都一样,除了标识符由命名空间的名称修饰之外。

From the C++ compiler's point of view, is namespace just a name decoration convention? I have inspected the generated assembly listing and found that everything just looks the same except the identifiers are decorated by the namespace's name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

篱下浅笙歌 2024-11-22 04:02:13

正如您所指出的,名称修改是故事的一部分(但这样做的原因更多地与链接器而不是编译器有关)。

然而,就编译器中命名空间的处理而言,名称修饰远非全部。除此之外,编译器必须能够找出不合格的名称,这可能很重要:请参阅 依赖于参数的查找

As you point out, name mangling is part of the story (but the reasons for doing it have more to do with linkers rather than compilers).

However, name mangling is far from the whole story as far as the handling of namespaces in the compiler is concerned. Among other things, the compiler has to be able to figure out unqualified names, which can be non-trivial: see argument-dependent lookup.

紧拥背影 2024-11-22 04:02:13

据我所知就是这样。描述可以在名称修改下找到:
http://en.wikipedia.org/wiki/Name_mangling

As far as I know that's what it is. The desciption can be found under name mangling:
http://en.wikipedia.org/wiki/Name_mangling

音栖息无 2024-11-22 04:02:13

从C++编译器的角度来看,命名空间只是一个名称修饰约定吗?

我想是的。它只是最后的一个名字装饰。

为了做到这一点,编译器做了很多事情。在解析名称时,它可能会从多个名称空间中选择正确的名称空间。

例如,

namespace X
{
  void f(); //compiler chooses X only when decorating f()
  namespace Y
  {
      void f();  //compiler chooses X and Y when decorating f()
      void g()   //compiler chooses X and Y when decorating g()
      {
          f();    //which f? Compiler decorates it with both X and Y.
          X::f(); //which f? Compiler decorates it with X only.
      }
  }
}

From the C++ compiler's point of view, is namespace just a name decoration convention?

I think yes. Its just a name decoration at the end.

In order to do that compiler does lots of things. It chooses the correct namespace(s), possibly out of many, when resolving a name.

For example,

namespace X
{
  void f(); //compiler chooses X only when decorating f()
  namespace Y
  {
      void f();  //compiler chooses X and Y when decorating f()
      void g()   //compiler chooses X and Y when decorating g()
      {
          f();    //which f? Compiler decorates it with both X and Y.
          X::f(); //which f? Compiler decorates it with X only.
      }
  }
}
画骨成沙 2024-11-22 04:02:13

Bjarne Stroustrup 编写的第一个 C++ 编译器被称为 CFront,这并非巧合。它将 C++ 代码转换为 C 代码并将其提供给 C 编译器。所以,我相信这只是名称修改来创建用于重载和重载的独特符号。避免名称冲突(命名空间)

It is no coincidence that the first C++ compiler written by Bjarne Stroustrup was called CFront. It converted C++ code to C and fed it to a C compiler. So, I believe it is just name mangling to create unique symbols for overloading & avoid name conflicts (namespace)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文