开发者

C++11 library features dependent on compiler specific proxys

开发者 https://www.devze.com 2023-04-04 16:48 出处:网络
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

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 wou开发者_StackOverflow中文版ld 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.


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 that cin and cout 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号