【发布时间】:2014-03-20 22:19:27
【问题描述】:
我正在尝试使用 constexpr 实现编译时哈希算法。我下载了 2013 年 11 月的 CTP,因为它支持 constexpr,但那是谎言......
#define hashCharacter(T, J) (((T >> 0x0D) | (T << 0x13)) + J)
unsigned long constexpr GetHashCompile(const char * asSource, unsigned long asValue = 0)
{
return asSource[0] == '\0'
? asValue
: GetHashCompile(asSource + 1, hashCharacter(asValue, asSource[0]));
}
int main(int a, char ** b)
{
const auto value = GetHashCompile("Hello from compiler");
printf("%d", value);
}
GetHashCompile 不会在编译时而不是运行时生成。我如何使用 Visual Studio 完成上述代码?相同的代码使用 GCC 或 CLANG 完美运行。
【问题讨论】:
-
这个编译器显然不接受字符串字面量参数
-
试试
constexpr auto value = GetHashCompile("Hello from compiler");,它实际上需要在编译时进行评估。 -
两者都不起作用,现在输出编译错误。表示该值不是常数,无法计算。
标签: visual-studio c++11 constexpr