I am currently working on a code project that requires me to replace some strings with hashes of those strings. Since these lines will not change at runtime, it would be useful, in terms of efficiency, for the c preprocessor to execute my hash function for every line that I declare hashed at compile time.
Is there a way to get the C preprocessor to run my hash function at compile time?
I know that this does not work as I described above, but just to understand where I am going, here is some kind of pseudo code that uses a macro. Imagine that instead of simply expanding the macro, the preprocessor ran the hash function and expanded it to the return value of this hash function:
#include <iostream> #include <string> #define U64_HASH(inputString) getU64HashCode(inputString) //my hash function unsigned long long getU64HashCode (string inputString) { /*code*/ } int main() { cout << U64_HASH("thanks for helping me") << endl; return 0; }
Again, ideally cout << U64_HASH("thanks for helping me") << endl; will expand to cout << 12223622566970860302 << endl;
I wrote a header file generator and it works great for this project.
Final decision
I decided to use the John Purdy perl script for this project, since it is just awesome and allows me to feed the output I want directly to my compiler. Thanks a lot, John.
c ++ c-preprocessor
Michael taufen
source share