You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2022 (17.11.0) C++ constexpr疑似Bug确认问询

VS2022 constexpr指针求值编译错误问题记录

这段代码在VS2019及GCC环境中可正常编译运行,但在VS2022 17.11.0版本中编译失败,报错信息为:error C2131: expression did not evaluate to a constant,失败原因是编译器无法计算指针值。

代码示例

constexpr bool contains(const char strz[], char ch) noexcept {//search a char in a 0-terminated string
    for(auto*c=strz; *c; ++c){
        if(*c == ch){
            return true;
        }
    }
    return false;
}

struct S{
    static constexpr const char* const arr[] = {
        "abc",
        "*12"  // <-- comment this to get a compile error on VS2022
    };
};

template<typename S>
class Cls{
public:
    static constexpr auto func() noexcept {
        for(const auto& s: S::arr){
            if(contains(s, '*')){
                return true;
            }
        }
        return false;
    }
};

int main(){
    constexpr auto r = Cls<S>::func();// <-- remove constexpr to have it work also with only 1 element in "arr"
    return r;
}

异常现象

  • 当arr数组仅包含一个元素时编译失败,包含多个元素时可正常运行
  • 移除main函数中r的constexpr修饰符后,单元素情况也能正常编译运行
  • 将arr移出结构体定义,或者显式指定数组大小,两种情况下均无编译问题

处理进展

经Stack Overflow成员确认问题后,已向微软提交工单,微软回复该问题的修复已在内部完成,等待正式发布。

内容的提问来源于stack exchange,提问作者Solo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 09:57:18