Compile-time (preprocessor) hashing of string
Compile-time (preprocessor) hashing of string
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.
Answer by Eddy Pronk for Compile-time (preprocessor) hashing of string
This can be done with Boost.MPL but it might not be the type of hash you are after.
http://arcticinteractive.com/2009/04/18/compile-time-string-hashing-boost-mpl/
Answer by anon for Compile-time (preprocessor) hashing of string
Why not make generating the hash part of your build process? You can write a simple batch file to generate the hash (assuming you have a program to do such a thing - if not, write one) and have it output a preprocessor directive something like:
#define MY_HASH 123456789
to a .h file that then gets #included in your application.
Answer by Georg Fritzsche for Compile-time (preprocessor) hashing of string
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):
foo = hash<3DES, str<'a','b','c'> >::result; // or: foo = hash<3DES, str<'abc','def'> >::result;
... or simply do it as a pre-build step.
Answer by nategoose for Compile-time (preprocessor) hashing of string
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?".
Answer by leetNightshade for Compile-time (preprocessor) hashing of string
I stumbled across a
Also, here's a short version of a JSHash function using the above technique. The shown one here supports up to 4 characters, though you can add as many as you want.
template struct cHash { template struct HashCalc { enum { value = (C == 0) ? hash : hash ^ ((hash << 5) + C + (hash >> 2)) }; }; enum { value = HashCalc::value>::value>::value>::value }; };
As noted, because this is a compile time hash, you can do something like this:
namespace Section { enum Enum { Player = cHash<'p','l','a','y'>::value }; }
It's not the most elegant solution, so I plan on doing more research in this area, however as this is the only thing I've gotten to work in VisualStudio2010 I'm a little limited as far as my current project is concerned.
Answer by michaeljt for Compile-time (preprocessor) hashing of string
While this is not a proper answer to the question, see this blog entry for an example of a hash function for strings of up to 256 characters implemented purely as a C macro:
http://lolengine.net/blog/2011/12/20/cpp-constant-string-hash
Here is the actual code from the blog:
#include #include #include #define H1(s,i,x) (x*65599u+(uint8_t)s[(i)>16)))
If you know ahead of time that you will only use it for static strings you could replace strlen() with sizeof().
Answer by Ian Ni-Lewis for Compile-time (preprocessor) hashing of string
Answers claiming that strings cannot be parsed at compile time are wrong. Character pointers can't be parsed at compile time, but string literals are not character pointers; they are arrays of characters whose length is part of the type. It's easy to forget that, because in most cases it's far more useful to let them decay into a char*. But they don't start out that way.
Ah, but how to actually define a function that takes a fixed-length character array, especially if we'd really rather use it on strings of arbitrary length? That's where template argument deduction comes in super handy:
template constexpr int hash(const char (&str)[L], int n = L - 1) { // hash goes here. You can define recursively as a // function of str[n] and hash(str, n-1). Remember to // treat 0 as a special case. }
That should get you started. Obviously the hash itself needs to be simple enough for compile-time computation, but that's probably ok.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment