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
相关产品推荐
相关产品推荐

