0

I have read llvm coding style. It say "Do not use Static Constructors". I don't understand "Static Constructor" here. It gives an example that global variables are "Static Constructor".

  1. Is there any else "Static Constructor"?
  2. I know "static initialization order fiasco". Does "Do not use Static Constructors" mean "Do not use global variables"? What is the substitute?
  3. LLVM wants to purge all "Static Constructor" in its project to solve the problem. How do they do it perfectly? I mean "static initialization order fiasco" will always exist if we use global variables. Even we apply "Construct On First Use", it still causes new problems. What's LLVM's perfect solution?
Patrick
  • 141
  • 9
  • Highly related: https://stackoverflow.com/questions/5803953/static-constructor-in-c – Rakete1111 Jul 15 '17 at 11:02
  • @Rakete1111 I have read that before. It does not answer my question. – Patrick Jul 15 '17 at 11:07
  • Well it does answer the question 1), because you don't seem to know what they are (they don't exist). Your question is probably a bit too broad in my opinion though. – Rakete1111 Jul 15 '17 at 11:08
  • 1
    the llvm doc you linked says: "global variables whose types have a constructor or destructor". Which is the part that you don't understand? – geza Jul 15 '17 at 11:12
  • @Rakete1111 No, I do know it does not exist. I use double quotes here. "Static Constructor" referred here is the "Static Constructor" referred in [llvm coding style](http://llvm.org/docs/CodingStandards.html#do-not-use-static-constructors). – Patrick Jul 15 '17 at 11:13
  • @geza “global variables” is just one of them. I want to know if there is any else "Static Constructor". – Patrick Jul 15 '17 at 11:28

1 Answers1

1
  1. Static constructor is an unfortunate name here. I think that they mean here any variable initialization which will be run before main(). For example, a global variable like this:int a = fn();. It will call fn() before main().

  2. No, it doesn't mean that. If a global variable is statically initialized, it avoids the fiasco. For example, int a = 2 will be statically initialized. Or a global object with constexpr constructor will be statically initialized too.

  3. You must use global variables which can be statically initialized. Or you can just remove all global variables altogether. In my opinion, almost all global variables are indicators of bad design, they should be avoided if it is possible. I don't know LLVM's exact solution for this, though.

geza
  • 28,403
  • 6
  • 61
  • 135
  • I think your point in 2) is do not create a global object that depends on another global object which is not const? – Patrick Jul 15 '17 at 12:14
  • @CppLucifer: instead of "const" is "which is not statically initialized". But this is not 100% true either. In a compilation unit, the order which global objects dynamically initialized, is defined. So it is fine to depend on another global object, if it is in the same compilation unit, and defined before. – geza Jul 15 '17 at 12:23