[英]error: constexpr variable 'struct2Var' must be initialized by a constant expression
The following program is failing when compiling with Clang with error: constexpr variable 'struct2Var' must be initialized by a constant expression {var, 2100433} .以下程序在使用 Clang 编译时失败并出现错误: constexpr 变量 'struct2Var' must be initialized by a constant expression {var, 2100433} 。
If I remove __attribute__((weak))
from "var" declaration, it is passing without any issues.如果我从“var”声明中删除
__attribute__((weak))
,它会毫无问题地通过。
Can somebody please explain the theory/reason behind this error.有人可以解释这个错误背后的理论/原因吗?
struct myStruct
{
public:
constexpr operator const wchar_t*() const
{
return &m_cch;
}
const wchar_t m_cch;
};
extern __attribute__((weak)) const constexpr myStruct var {'a'};
struct myStruct2
{
const wchar_t* stzKey = nullptr;
int intvar = 0;
};
static constexpr const myStruct2 struct2Var[1]
{
{var, 2100433}
};
It looks like using __attribute__((weak))
discards the constexpr
qualifier with clang but not with gcc.看起来使用
__attribute__((weak))
会丢弃带有 clang 而不是 gcc 的constexpr
限定符。 Despite clang trying to be a drop in replacement for gcc, it might implement such non standard feature differently.尽管 clang 试图成为 gcc 的替代品,但它可能会以不同的方式实现这种非标准功能。 In that case, I would say that neither gcc nor clang is wrong.
在那种情况下,我会说 gcc 和 clang 都没有错。
Also, global constexpr
might be tricky to maintain as they should all be defined consistently in every translation unit.此外,全局
constexpr
可能难以维护,因为它们应该在每个翻译单元中都被一致地定义。 To face this issue, inline variable have been added to c++17.为了解决这个问题,C++17 中添加了内联变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.