C++11 库功能依赖于编译器特定代理
通过反复试验,我注意到某些 C++11 功能依赖于某种编译器魔法 (TM)。我一直在努力实现我自己的符合标准的 stdlib。我知道 stdlib 有现成的实现,但这个版本更多的是我自己针对小型应用程序的个人版本。
昨晚我根据规范实现了 std::initializer_list ,但我在使其工作时遇到了麻烦,我到处寻找答案,结果却留下了这样的陈述:这是不可能的,而且它会需要修改编译器本身。好吧,我决定看一下它当前的 libstdc++ 实现,果然我的实现在设计上完全相同,只是边缘略有不同,我浪费了毫无意义的时间来弄清楚为什么要实现当它的设计与 libstdc++
中的设计完全相同时,它不起作用。直到六个小时后我才意识到它必须位于命名空间 std 中。事实证明,该实现是 merley 编译器的代理,这使得 initializer_list
成为可能,并且编译器本身在 namespace std
中搜索 initializer_list
类。
我的问题是,是否还有其他我应该注意的库功能需要某种特殊的编译器魔法才能工作、任何更多隐藏的代理连接或为任何新的 C++11 库功能秘密编译器特性? 我想提前知道这些,这样我就可以在实现其他依赖于编译器魔法的功能时做好准备,而不是浪费一整天的时间通过反复试验来弄清楚;这可能会变得乏味且相当烦人。
谢谢。
It's been brought to my attention by trial and error that there are certian C++11 features that depend on some sort of compiler magic (TM). I've been messing around with implementing my own standard conformant stdlib. I know there are readily available implementations of the stdlib, but this one is more of my own personal version for small applications.
Last night I implemented std::initializer_list
according to spec, and I was having trouble making it work, I searched high and low for anwers only to be left with statements that it was impossible, and that it would require modifications to the compiler itself. Well I decided to take a look at the current libstdc++
implementation of it, and sure enough my implementation was exactly the same in design, just slightly different around the edges, I wasted pointless hours figuring out why the implementation was not working, when it was exactly the same in design as the one in libstdc++
. It was not untill six hours in before I realised it had to be in namespace std. Turns out the implementation is merley a proxy to the compiler which makes the initializer_list
possible, and the compiler itself searches for the initializer_list
class in namespace std
.
My question is are there anymore other library features I should be aware of that require some sort of special compiler magic to work, any more hidden proxy connections, or secrete compiler intristics for any of the new C++11 library features?
I would like to know these in advance so I can be prepared for when I do implement other functionality that depends on compiler magic, instead of wasting a whole day figuring it out by trial and error; which can get tedious and rather annoying.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
中的函数主要是编译器的钩子。如果没有编译器的帮助,一些
属性查询是不可能或很难实现的。即使只是困难的那些也可能在编译器的本机 stdlib 由于“作弊”而成功的情况下发生故障。当然,它们的编译速度也会更快。尽管可以编写
使得cin
和cout
在首次使用时自动初始化,但大多数编译器选择作弊并链接它们使得它们首先处于静态初始化中。当然,
必须与编译器期望的完全匹配。可能还有更多我不知道或者想不到的事情。
除了stdlib“作弊”和依赖编译器之外,编译器还可能依赖stdlib中存在的非标准函数。因此,即使您实现了所有标准功能,您也可能必须复制粘贴例程来遍历异常表、处理虚拟析构函数中的层次结构等。
The functions in
<exception>
are mostly hooks to the compiler.Several
<type_traits>
property queries are impossible or very hard to implement without compiler assistance. Even the ones that are merely difficult might malfunction where the compiler's native stdlib succeeds due to "cheating." And of course they will compile faster as well.Although it's possible to write
<iostream>
such thatcin
andcout
are initialized automatically upon first use, most compilers choose to cheat and link them such that they are first in static initialization.<typeinfo>
, of course, must exactly match what the compiler expects.There are probably more things that I don't know or can't think of.
Besides the stdlib "cheating" and relying on the compiler, the compiler may also rely on nonstandard functions existing in the stdlib. So even if you implement all the standard functionality, you will likely have to copy-paste routines for walking exception tables, handling hierarchy in virtual destructors, etc.
所有“神奇”类型(编译器所需的类型)都列在 C++ 规范中名为“语言支持库”的特殊部分中。显然,如果您正在实现 C++ 标准库,那么您应该手头有一份标准的副本。初始化列表位于该部分,还有
type_info
、
、全局运算符 new/delete、
、等等。不要尝试实施这些;使用编译器给你的东西。
All of the "magic" types, the ones needed by the compiler, are listed in their own special section of the C++ specification called "Language support library". Obviously, if you're implementing the C++ standard library, you should have a copy of the standard handy. Initializer lists are in that section, as well as
type_info
,<cstdint>
, the global operators new/delete,<exception>
, and so forth.Do not try to implement these; use what the compiler gives you.