C++ 中真正的编译时字符串哈希
基本上我需要一个真正的 C++ 编译时字符串哈希。我不关心技术细节,可以是模板、宏,任何东西。到目前为止我见过的所有其他散列技术只能在编译时生成散列表(如 256 个 CRC32 散列),而不是真正的散列。
换句话说,即使在调试版本中,我也需要将其
printf("%d", SOMEHASH("string"));
编译为(在伪汇编程序中)
push HASHVALUE
push "%d"
call printf
,并且对字符串没有运行时操作。我正在使用 GCC 4.2 和 Visual Studio 2008,我需要适合这些编译器的解决方案(所以没有 C++0x)。
Basically I need a truly compile-time string hashing in C++. I don't care about technique specifics, can be templates, macros, anything. All other hashing techniques I've seen so far can only generate hashtable (like 256 CRC32 hashes) in compile time, not a real hash.
In other words, I need to have this
printf("%d", SOMEHASH("string"));
to be compiled as (in pseudo-assembler)
push HASHVALUE
push "%d"
call printf
even in Debug builds, with no runtime operations on string. I am using GCC 4.2 and Visual Studio 2008 and I need the solution to be OK for those compilers (so no C++0x).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题在于,在 C++03 中,对字符串文字添加下标(即访问单个字符)的结果不是适合用作模板参数的编译时常量。
因此不可能这样做。我建议您编写一个脚本来计算哈希值并将其直接插入源代码中,即
printf("%d", SOMEHASH("string"));
转换为
printf("%d", 257359823 /*SOMEHASH("字符串")*/ ));
The trouble is that in C++03 the result of subscripting a string literal (i.e. access a single character) is not a compile-time constant suitable for use as a template parameter.
It is therefore not possible to do this. I would recommend you to write a script to compute the hashes and insert them directly into the source code, i.e.
printf("%d", SOMEHASH("string"));
gets converted to
printf("%d", 257359823 /*SOMEHASH("string")*/ ));
编写您自己的预处理器,扫描源中的 SOMEHASH("") 并将其替换为计算出的哈希值。然后将其输出传递给编译器。
(类似的技术也用于 I18N。)
Write your own preprocessor that scans the source for SOMEHASH("") and replaces it with the computed hash. Then pass the output of that to the compiler.
(Similar techniques are used for I18N.)
对于模板,只有以下语法才有效:
SOMEHASH<'s','t','r','i','n','g'>
看到这个例如:
http://arcticinteractive.com/2009/04 /18/compile-time-string-hashing-boost-mpl/
或者
编译时字符串哈希
With templates only the following syntax will work:
SOMEHASH<'s','t','r','i','n','g'>
see this eg:
http://arcticinteractive.com/2009/04/18/compile-time-string-hashing-boost-mpl/
or
compile-time string hashing
为此,您必须等待 C++0x 中的用户定义文字。
You have to wait for user-defined literals in C++0x for this.
如果您不介意在代码中使用新的 C++0x 标准(一些答案还包括在旧的 C++03 标准中工作的内容的链接),这些问题之前已经在 StackOverflow 上提出过:
字符串的编译时(预处理器)哈希
编译时字符串哈希
这两个内容都包含答案,可帮助您弄清楚如何实现此功能。
这是一篇博客文章,展示了如何使用 Boost .MPL 编译时字符串哈希
If you don't mind using the new C++0x standard in your code (some answers also include links to stuff that works in the older C++03 standard), these questions have been asked before on StackOverflow:
Compile-time (preprocessor) hashing of string
Compile time string hashing
Both of those contain answers that will help you figure out how to possibly implement this.
Here is a blog post that shows how to use Boost.MPL Compile Time String Hashing
这是不可能的,它可能在 C++0x 中,但绝对不可能在 C++03 中。
That's not possible, it might be in C++0x but definitely not in C++03.