是否可以去掉C++11用户定义的文本?
假设我有一个用户定义的文本,用于在编译时计算char[]的哈希代码:是否可以去掉C++11用户定义的文本?,c++11,strip,user-defined-literals,C++11,Strip,User Defined Literals,假设我有一个用户定义的文本,用于在编译时计算char[]的哈希代码: constexpr unsigned operator "" _hash(const char * pStr, std::size_t length) { // Some code here } 我可以像这样使用它: unsigned hash = "Hello"_hash; std::cout << hash; 我只想把散列代码保存在二进制文件中,去掉Hello,因为在散列计算之后它是无用的。如何实现
constexpr unsigned operator "" _hash(const char * pStr, std::size_t length)
{
// Some code here
}
我可以像这样使用它:
unsigned hash = "Hello"_hash;
std::cout << hash;
我只想把散列代码保存在二进制文件中,去掉Hello,因为在散列计算之后它是无用的。如何实现这一点?无需采取特殊行动。编译器会注意到该值在优化阶段未被使用,并为您删除它 确保您是使用-O1或更高版本编译的,并且您不应该在二进制文件中看到Hello弹出窗口 按照GCC 4.9.2进行测试
constexpr ULONG GenHashTableValue(CONST INT C, USHORT Hi, USHORT Lo, ULONG Seed = 0x00100001) {
return C == 0 ? (Hi << 0x10) | Lo : (
Seed = (Seed * 125 + 3) % 0x2aaaab,
Hi = Seed & 0xffff,
Seed = (Seed * 125 + 3) % 0x2aaaab,
Lo = Seed & 0xffff,
GenHashTableValue(C - 1, Hi, Lo, Seed)
);
}
constexpr ULONG Index2Count(ULONG N) {
return GenHashTableValue(N / 0x100 + (N % 0x100) * 5 + 1, 0, 0);
}
constexpr ULONG HashInternal(const void* Data, const SIZE_T Size, HashType HashType, ULONG Seed1 = HashSeed1, ULONG Seed2 = HashSeed2) {
return Size == 0 ? Seed1
: (Seed1 = Index2Count((HashType << 8) + ToUpper(Data)) ^ (Seed1 + Seed2),
Seed2 = ToUpper(Data) + Seed1 + Seed2 + (Seed2 << 5) + 3,
HashInternal((PUCHAR)Data + 1, Size - 1, HashType, Seed1, Seed2));
}
// utf8 string
constexpr ULONG operator "" _hashO(const char* Str, size_t Size) {
return HashInternal(Str, Size, HashOffset);
}
vc2015使用mpq哈希。constexpr unsigned hash=…?谢谢。O1成功了。我使用的是clang,我想我还需要-dead_strip