Use C++ Global Constants with Caution

What are the ramifications of C++ global constants?

Item 2 of the Effecitve C++ recommends against #defines. This is very good advice in general; however…

The execution order of static initializers is undefined; see static initialization order fiasco.

A ramification in program start-up time is discussed in this thread of Chromium development–even seemingly innocent constant definitions may affect the start-up time. Consider this snippet:

extern const int b;
const int a = b + 1;

In this case, compiler still needs to generate static initializers for a because the value of b is unknown at compile time.

Should we consider banning global constants entirely?

Creative Commons License
This blog by Che-Liang Chiou is licensed under a Creative Commons Attribution 4.0 International License.