简体   繁体   English

错误:constexpr 变量“struct2Var”必须由常量表达式初始化

[英]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.

相关问题 错误! constexpr 变量必须由常量表达式 constexpr 初始化 - Error! constexpr variable must be initialized by a constant expression constexpr 错误:Constexpr 变量必须由常量表达式初始化,但它是 - ERR: Constexpr variable must be initialized by a constant expression, but it is constexpr 变量必须由常量表达式初始化 - constexpr variable must be initialized by a constant expression 我不断从以前使用的工作代码中收到“错误:constexpr 变量'x'必须由常量表达式初始化” - I keep getting an "error: constexpr variable 'x' must be initialized by a constant expression" from previously used working code 当值是非常量但使用常量表达式初始化时使用constexpr吗? - Using constexpr when a value is non-const but initialized with a constant expression? 错误:当变量是常量时,表达式必须具有常量值? - Error: Expression must have a constant value, when variable is constant? constexpr initializer_list 引发错误:“表达式必须具有常量值——引用或指向具有有限生命周期的临时指针” - constexpr initializer_list raising an error: "expression must have a constant value -- reference or pointer to temporary with limited lifetime" constexpr 编译器错误 - 不可用于常量表达式 - constexpr compiler error - is not usable in a constant expression 使用从未打算在常量表达式中使用的 constexpr 变量是否有好处? - Are there benefits to using a constexpr variable that is never intended to be used in a constant expression? 为什么在 Clang 中出现此错误? “如果条件不是常量表达式,则为 constexpr” - Why do I get this error in Clang? "constexpr if condition is not a constant expression"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM