purpose
Say you want the following:
STATIC_EXECUTE { printf("This probably prints first"\n"); } STATIC_EXECUTE { printf("But order isn't guaranteed in the language spec, IIRC"\n"); } int main(int argc, char **argv) { printf("This definitely prints last. Buh Bye.\n"); }
Implementation
C ++ version - static variable + constructor:
// This is some crazy magic that produces __StaticExecute__247 // Vanilla interpolation of __StaticExecute__
Version C - Static Variable + Function
Notes
IMHO, the C ++ version is a bit more elegant. In theory, it consumes a little less space. Otherwise, potatoes, tat-o.
Caution: I have not tested the version of "C" on my own C-only compiler. Crossed fingers; post a note if it doesn't work.
Caution: compiler compatibility in general is a complex thing. I would not be shocked if there is an error in the compiler.
The code BOOST_PP_CAT is stolen from boost / preprocessor / cat.hpp . I simplified the implementation, and portability might have been violated in this process. If this does not work, try the original (more detailed) implementation and post a comment below. Or, if you are already using Boost, you can simply use their version.
If you are trying to understand the magic of Boost, note that (at least for me in this scenario too) the following also works:
Dave dopson
source share