Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to have the C preprocessor execute code during compilation?
    primarykey
    data
    text
    <p>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.</p> <p>Is there any way to get the <strong>C</strong> preprocessor to run my hash function at compile time?</p> <p>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:</p> <pre><code> #include &lt;iostream&gt; #include &lt;string&gt; #define U64_HASH(inputString) getU64HashCode(inputString) //my hash function unsigned long long getU64HashCode (string inputString) { /*code*/ } int main() { cout &lt;&lt; U64_HASH("thanks for helping me") &lt;&lt; endl; return 0; } </code></pre> <p>Again, ideally the <code>cout &lt;&lt; U64_HASH("thanks for helping me") &lt;&lt; endl;</code> would expand to <code>cout &lt;&lt; 12223622566970860302 &lt;&lt; endl;</code></p> <p><strike>I wrote a header file generator, and that works fine for this project.</strike></p> <p><strong>Final Solution</strong></p> <p>I have decided to use <a href="https://github.com/evincarofautumn/macript" rel="nofollow">John Purdy's perl script</a> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload