Is there any way to create a hash of string at compile time using the C/C++ preprocessor (or even template-metaprogramming)?
e.g. UNIQUE_SALT("HelloWord", 3DES);
The idea is that HelloWorld will not be present in the compiled binary, just a hash.
Edit: There are many of these declarations spread over a large codebase.
With C++0x, this is possible as covered by answers in #1 and #2.
In C++03 there was no compile time string processing. With the preprocessor you can't seperate the string into tokens, with templates you can't access single characters. There was however a discussion on the speculated approach using C++0x.
What you could do for C++03 is to pass the string character-wise (possible using multi-character literals):
... or simply do it as a pre-build step.
Even if this can't be (reasonably) done with the preprocessor, if you used a string literal or declared it as
static const
and did not create any lasting references to it the compiler will likely go ahead and do all of the math to generate the result and omit the string in the object file if you compile with optimizations. The hardest part of this is that you can't make the code to initialize a global or static variable too complicated or the compiler will say "hey, you! Don't you know you can't use a for loop outside of a function?".