【问题标题】:constexpr won't work using Visual C++ Compiler Nov 2013 CTP (CTP_Nov2013)constexpr 无法使用 Visual C++ 编译器 Nov 2013 CTP (CTP_Nov2013)
【发布时间】: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


【解决方案1】:

实际上,2013 年 11 月的 CTP 并没有声称完全支持constexpr,而只是声称部分支持constexprfeatures list 明确地告诉 constexpr 不支持成员函数和数组。由于字符串文字是一种数组,因此也不支持它们:

CTP 支持 C++11 constexpr,但成员函数除外。 (另一个限制是不支持数组。)此外,它不支持 C++14 的扩展 constexpr 规则。

【讨论】:

    猜你喜欢
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多