编译时(预处理器)散列字符串 [英] Compile-time (preprocessor) hashing of string

查看:87
本文介绍了编译时(预处理器)散列字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在编译时使用 C / C ++预处理器(甚至是模板元编程)来创建字符串散列?



例如 UNIQUE_SALT(HelloWord,3DES);



这个想法是HelloWorld不会出现在编译好的二进制文件中,只是一个散列。



编辑:这些声明中有许多声明遍布在一个大的代码库中。

解决方案

使用C ++ 0x,这可能在#1 中的答案和#2



在C ++ 03没有编译时字符串处理。使用预处理器时,不能将字符串分隔为令牌,而模板则无法访问单个字符。然而,有人对使用C ++ 0x的推测方法进行了讨论。



你可以为C ++ 03做什么,是以字符方式传递字符串(可能使用多字符文字):

  foo = hash< 3DES,str<'a','b','c'> > ::结果; 
//或:
foo = hash< 3DES,str<'abc','def'> > ::结果;

...或者直接将其作为预构建步骤。


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):

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.

这篇关于编译时(预处理器)散列字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆