C++编译器实现命名空间
从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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如您所指出的,名称修改是故事的一部分(但这样做的原因更多地与链接器而不是编译器有关)。
然而,就编译器中命名空间的处理而言,名称修饰远非全部。除此之外,编译器必须能够找出不合格的名称,这可能很重要:请参阅 依赖于参数的查找。
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.
据我所知就是这样。描述可以在名称修改下找到:
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
我想是的。它只是最后的一个名字装饰。
为了做到这一点,编译器做了很多事情。在解析名称时,它可能会从多个名称空间中选择正确的名称空间。
例如,
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,
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)