21. Avoid initialization dependencies across compilation units

Summary

Keep (initialization) order: Namespace-level objects in different compilation units should never depend on each other for initialization, because their initialization order is undefined. Doing otherwise causes headaches ranging from mysterious crashes when you make small changes in your project to severe non-portability even to new releases of the same compiler.

Discussion

When you define two namespace-level objects in different compilation units, which object’s constructor is called first is not defined. Often (but not always) your tools might happen to initialize them in the order in which the compilation units’ object files are linked, but this assumption is usually ...

Get C++ Coding Standards: 101 Rules, Guidelines, and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.