Take the 2-minute tour ×

I'm currently working on a code project that requires me to replace certain strings with hashes of those strings. Seeing as these strings will not change at runtime, it would be advantageous, efficiency wise, to have the c preprocessor run my hash function on each string that I declare to be hashed at compile time.

Is there any way to get the c preprocessor to run my hash function at compile time?

I know this doesn't work the way I described above, but just to get an idea of where I'm going, here's some pseudocode 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 that hash function:

    #include <iostream>
    #include <string>

    #define U64_HASH(inputString) getU64HashCode(inputString)

    //my hash function
    unsigned long long getU64HashCode (string inputString)

    int main()
        cout << U64_HASH("thanks for helping me") << endl;
        return 0;

Again, ideally the cout << U64_HASH("thanks for helping me") << endl; would expand to cout << 12223622566970860302 << endl;

I wrote a header file generator, and that works fine for this project.

Final Solution

I have decided to use John Purdy's perl script for this project, as it is simply awesome,and allows me to feed the output I want directly to my compiler. Thanks very much, John.

share|improve this question
Not without some crazy magic. –  Rafe Kettler Sep 30 '11 at 2:11
C++11 has user defined literals and constexpr. Those might be helpful. –  Pubby Sep 30 '11 at 2:18
you could always #define those strings as their hashes? AFAIK the C preprocessor does not have any ability to run code. –  Serdalis Sep 30 '11 at 2:21
The built-in C preprocessor can't do this. MSN's answer is the closest you're going to get. –  Zack Sep 30 '11 at 2:40
For future reference, I wrote a very handy Perl script for just this sort of task. –  Jon Purdy Oct 1 '11 at 0:53
show 1 more comment

4 Answers

up vote 6 down vote accepted

One way to accomplish this is to put all your strings into a header file, and name them:

// StringHeader.h
#define   helloWorld              "Hello World"
#define   error_invalid_input     "Error: Invalid Input"
#define   this_could_get_tedious  "this could get tedious"

Then you can use those strings:

#include "StringHeader.h"
std::cout << this_could_get_tedious << std::endl;

Then you can run a program on your StringHeader.h to hash each string, and generate a replacement header file:

// Generated StringHeader.h
#define   helloWorld              097148937421
#define   error_invalid_input     014782672317
#define   this_could_get_tedious  894792738384

That looks very manual and tedious, at first, but there's ways to automate it.

For example, you could write something to parse your source code, looking for "quoted strings". Then it could name each string, write it to a single StringHeader.h, and replace the inline quoted string with the new named string constant. As an additional step when you create the file, you could hash each string - or you could has the file in one go after you've created it. This could let you create a hashed and non-hashed version of the file (which could be nice to create a non-hashed Debug version, and a hashed Release version).

If you do try that, your initial parser to look for strings will have to handle edge cases (comments, #include lines, duplicated strings, etc).

share|improve this answer
add comment

If a compiler ever supports this, C++11 has user defined literals:

constexpr unsigned long long operator "" U64_HASH_(
    const char *literal_string) { ... }

#define U64_HASH(inputString) inputString U64_HASH_

or with constexpr:

constexpr unsigned long long operator "" U64_HASH(
    const char *literal_string) { ... }
share|improve this answer
add comment

If you can't get the pre-processor to do this for you, you may be able to write your own pre-processor to do this step first.

share|improve this answer
Yeah, I'm considering that and will do so if necessary, but I really want to use the standard preprocessor if I can. –  Michael Taufen Sep 30 '11 at 2:19
add comment

There's no way to force it, but if your compiler is good enough, it can do it. Play with its optimization options and study the disassembly of the code in the debugger to see if any one of them lets you achieve what you want.

share|improve this answer
Can you name a single compiler that will do what you are talking about? –  David Nehme Sep 30 '11 at 2:23
All you have to do write the complete hash in the macro itself - then the compiler should see it as a constant expression and substitute the value (assuming the optimization settings allow it). Easy!* (* hard) –  geofftnz Sep 30 '11 at 2:40
@David: I think DMD compiler can do that via CTFE :) Though I'm also curious if it's possible in C++. Never heard of it –  Alexander Malakhov Sep 30 '11 at 2:50
@geofftnz: +1 for Easy!* part. Really funny :) –  Alexander Malakhov Sep 30 '11 at 2:51
@DavidNehme: I have not tried this code with many compilers, but I have seen some very good optimizations done by MSVC++ and gcc and from that I can't say it's something impossible. Maybe if the string is passed as const char* instead of string, some can figure it out. –  Alexey Frunze Sep 30 '11 at 2:53
show 1 more comment

Your Answer


