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

Clang在constexpr函数中拒绝非显式枚举值,GCC可正常编译

Clang与GCC在constexpr枚举转换上的编译差异

以下代码中,Clang仅会在constexpr版本的函数中编译失败:

enum foo { bar };

auto baz() { return static_cast<foo>(-1); }
constexpr auto barf() { return static_cast<foo>(-1); }

GCC可以正常编译两个版本的函数。请问这是Clang的bug吗?如果不是,为何仅在constexpr上下文会编译失败?

更新1
如果为枚举指定底层存储类型(如enum foo : char { bar };),Clang就能正常编译,但上述疑问仍未解决。

更新2
如果认为Clang的行为是正确的,那为何以下代码仍然编译失败:

enum foo { bar };

constexpr struct { int qux :1; } quux{-1};

auto baz() { return static_cast<foo>(quux.qux); }
constexpr auto barf() { return static_cast<foo>(quux.qux); }

根据C++17标准,foo应采用1位位域的存储方式,与quux.qux的存储一致,但Clang仍编译失败。

内容的提问来源于stack exchange,提问作者Intelligent Shade of Blue

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 22:58:16