我正在尝试使用 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 可以完美运行。
最佳答案
实际上,2013 年 11 月的 CTP 并没有声称完全支持 constexpr
,而只是声称部分支持 constexpr
。 features list明确告诉成员函数和数组不支持 constexpr
。由于字符串文字是一种数组,因此也不支持它们:
The CTP supports C++11
constexpr
, except for member functions. (Another limitation is that arrays aren't supported.) Also, it doesn't support C++14's extendedconstexpr
rules.
关于visual-studio - constexpr 无法使用 Visual C++ 编译器 2013 年 11 月 CTP (CTP_Nov2013),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22546369/