I have implemented an algorithm for which there is a strong use-case to know as soon as possible the maximum problem sizes that will be computed during the whole execution of the library so that the required memory (matrices, vectors etc) can be efficiently pre-allocated. “As soon as possible” defined as even earlier or no later than when the C++ static initialization triggers e.g.
/**
* Example of what I would like to accomplish but of course this
* System#getProperty is the Java way
*/
class texample {
static const int MAX_M_N;
}
const int texample::MAX_M_N = System.getProperty("maxMN"); // possible? how?
I could of course do this using dynamic initialization and allocation instead but it will be a lot simpler if these input argument properties are ‘seen’ during static initialization time. The reason being that the wrapper object instances statically-initialized/allocated will get destructed also by the static destruction process and I won’t have to worry about it e.g. handling exceptions and manually freeing/deleting the instances.
UPDATE: and one more thing 🙂 I’d like to have this without having to recompile. I can of course use a macro (I’m using CMake) so it would be very easy to change the macro in the CMakeList.txt file and re-compile but I’d prefer to have the sizes specified dynamically at runtime.
If reading from the environment is acceptable, you could try this:
Note that line “#1” is executed during the dynamic initialization phase of your program, which is the first time user code is executed, and it completes before
mainis called.