C++ static global non-POD: theory and practice

后端 未结 5 936
不思量自难忘°
不思量自难忘° 2021-02-15 02:11

I was reading the Qt coding conventions docs and came upon the following paragraph:

Anything that has a constructor or needs to run code to be initialized

相关标签:
5条回答
  • 2021-02-15 02:29

    The "not at all" part simply says that the C++ standard is silent about this issue. It doesn't know about shared libraries and thus doesn't says anything about the interaction of certain C++ features with these.

    In practice, I have seen global non-POD static globals used on Windows, OSX, and many versions of Linux and other Unices, both in GUI and command line programs, as plugins and as standalone applications. At least one project (which used non-POD static globals) had versions for the full set of all combinations of these. The only problem I have ever seen was that some very old GCC version generated code that called the dtors of such objects in dynamic libraries when the executable stopped, not when the library was unloaded. Of course, that was fatal (the library code was called when the library was already gone), but that has been almost a decade ago.

    But of course, this still doesn't guarantee anything.

    0 讨论(0)
  • 2021-02-15 02:29

    I don't think static objects constructors can be elided. There is probably a confusion with the fact that a static library is often just a bunch of objects which are token in the executable if they are referenced. Some static objects are designed so that they aren't referenced outside their containing object, and so the object file is put in the executable only if there is another dependency on them. This is not the case in some patterns (using a static object which register itself for instance).

    0 讨论(0)
  • 2021-02-15 02:38

    If the static object is defined in an object that does not get referenced, the linker can prune the object completely, including the static initializer code. It will do so regularly for libs (that's how the libc does not get completely linked in when using parts of it under gnu, e.g.).

    Interestingly, I don't think this is specific to libraries. It can probably happen for objects even in the main build.

    0 讨论(0)
  • 2021-02-15 02:43

    I see no problem with having global objects with constructors.

    They should just not have any dependency on other global objects in their constructor (or destructor).

    But if they do have dependencies then the dependent object must either by in the same compilation unit or be lazily evaluated so you can force it to be evaluated before you use it.

    The code in the constructor should also not be dependent on when (this is related to dependencies but not quite the same) it is executed, But you are safe to assume that it will get constructed at the very least (just before a method is called) and C++ guarantees the destruction order is the reverse of instantiation.

    Its not so difficult to stick to these rules.

    0 讨论(0)
  • 2021-02-15 02:52

    C++ doesn't define the order that static initializers execute for objects in different compilations units (the ordering is well defined within a compilation unit).

    Consider the situation where you have 2 static objects A and B defined in different compilation units. Let's say that object B actually uses object A in it's initialization.

    In this scenario it's possible that B will be initialized first and make a call against an uninitialized A object. This might be one thing that is meant by "not at all" - an object is being used when it hasn't had an opportunity to initialize it self first (even if it might be initialized later).

    I suppose that dynamic linking might add complexities that I haven't thought of that cuase an object to never be initialized. Either way, the bottom line is that static initializatino introduces enough potential issues that it should be avoided where possible and very carefully handled where you have to use it.

    0 讨论(0)
提交回复
热议问题