【问题标题】:static constexpr char m_pszFoo[] = "***FOO***" has compile error expression did not evaluate constantstatic constexpr char m_pszFoo[] = "***FOO***" has compile error expression did not evaluate constant
【发布时间】:2016-06-01 16:41:03
【问题描述】:

错误代码是 C2131。

即使我在其他文件中调用此变量的大小时,intelisense 也不会抱怨 intelisense 知道大小。我缺少什么才能让这个工作?任何帮助是极大的赞赏。在下面的图片中看到 intelisense 没有抱怨并且还在确定大小。

示例 a.h:

class a
{
private:
static constexpr char m_pszFoo[] = "***FOO***"; // compile error on this line C2131 expression did not evaluate to a constant

public:
a() { sizeof(m_pszFoo); }; // no compile error here
};

要重现所有你需要做的就是在任何其他文件中包含 a.h。没有包含它不会重现。

【问题讨论】:

  • @NathanOliver 在 msvc 2015 14.0.24720.00 update 1 中验证了上述示例
  • 我可以确认它不能在 MSVS 中编译。 clang 和 gcc 都可以编译。
  • 尝试制作构造函数constexpr
  • 嗨 @M.M constexpr 导致 sizeof 的另一个错误。 “语句可能不会出现在 constexpr 构造函数中。”这是你所期望的吗?如果是这样,它证明了什么?

标签: c++ visual-c++ visual-studio-2015


【解决方案1】:

所以我想出了一个解决 b\c 的方法,该错误消息使我似乎需要在某处添加一个 const

static constexpr char const m_pszFoo[] = "**FOO***";

出于某种原因,如果我们告诉编译器我们不应该能够更改字符串指向的字符,msvc 会很高兴。

* 编辑 *

我收到了一些建议,向微软提交错误并让他们进行调查,因为这看起来是与 msvc 的互操作错误。 这是 connect.microsoft 链接:https://connect.microsoft.com/VisualStudio/feedback/details/2781048

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-19
    • 2017-02-20
    • 2022-08-21
    • 2023-03-15
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多