Are static data members safe as C++ default arguments?
You still have to be worried about the static initialization order fiasco. Lets say you have a.cpp and b.cpp. In a.cpp you have
double const Thing::default_blarg = 0;
Now in a.cpp any call to run
after this point will have a initialized default and you are good to go. Unfortunately in b.cpp you have another static object that happens to create an instance of Thing
and call run
.
Now we do not know what will happen. If b.cpp runs first then default_blarg
is not initialized and we have undefined behavior.
Now for you second part
but I'm not sure when the default argument to run is initialized
The default arguments of a function are evaluated each time the function is called. So as long as the function is called after default_blarg
is initialized(and as noted above it may not) you will be okay.
From the C++11 Standard, Section 8.3.6/9:
Default arguments are evaluated each time the function is called.
As long as Thing::default_blarg
is initialized before Thing::run
is called, you should see predictable behavior.