Say I have an object that takes about 30 seconds to construct on each program run. This object is constructed with the same piece of data each and every time the program is run. What solution(s) exist for this problem?
EDIT:
I did some research tailored to a more specific version of my question. In particular, I wanted to populate a hash table with some values. I found http://www.gnu.org/software/gperf/ which addresses this exact issue. gperf generates a perfect hash function and the associated table to go along.
C++11 introduces an extended notion of “constant expression”, which you can confer on objects by means of the
constexprkeyword:Objects may have constructors declared as
constexpras well. The rules for functions to qualify as constant expressions are very restrictive, but if you are able to build such an object, then global objects which are constant expressions may indeed be computed and stored at compile time. They count as being “statically initialized” (basically, initialized before program start-up).Prior to C++11, only the simplest primitive types were eligible for such treatment.
Anything that requires dynamic allocation can never be a constant expression, so there’s no hope of having anything like a global
std::mapstatically initialized.