简体   繁体   English

constexpr 变量必须由常量表达式初始化

[英]constexpr variable must be initialized by a constant expression

I'm programming with C++Builder 10.3 from Embarcadero Technology which uses a c++17 compiler.我正在使用 Embarcadero Technology 的 C++Builder 10.3 进行编程,它使用 c++17 编译器。 I did not write the code below and it is a little more complex than I am used to working with.我没有编写下面的代码,它比我习惯使用的要复杂一些。 I have never used constexpr before.我以前从未使用过 constexpr 。

At the bottom of the code below when the compiler hits, "constexpr array g_PERT_S1" it gives the error "constexpr variable 'g_PERT_S1' must be initialized by a constant expression. non-constexpr function 'data' cannot be used in a constant expression array(226): declared here" Can you recommend a code change to resolve this error.在下面代码的底部,当编译器命中时,“constexpr array g_PERT_S1”会给出错误“constexpr variable 'g_PERT_S1' must be initialized by a constant expression. non-constexpr function 'data' cannot be used in a constant expression array (226):在此处声明“您能否建议更改代码以解决此错误。

struct AAPLUS_EXT_CLASS ELPMPP02PertubationsCoefficient
{
  double m_S;
  double m_C;
  array<int, 13> m_I;
};

constexpr array<ELPMPP02PertubationsCoefficient, 2> g_PERT_S3_4
{ {
 { -5.458720424980e-07,  2.801517894073e-07, {   0,   0,   2,   0,   0, -18,  16,   0,   0,   0,   0,   0,   0 } },
 { -5.121329506146e-07, -2.627345838573e-07, {   0,   0,   0,   0,   0,  18, -16,   0,   0,   0,   0,   0,   0 } }
} };


struct AAPLUS_EXT_CLASS ELPMPP02Pertubations
{
  const ELPMPP02PertubationsCoefficient* m_pTable;
  size_t m_nTableSize;
};


/*constexpr variable 'g_PERT_S1' must be initialized by a constant expression
non-constexpr function 'data' cannot be used in a constant expression array(226): declared here*/

constexpr array<ELPMPP02Pertubations, 4> g_PERT_S1  /*ERROR HERE*/
{ {
  { g_PERT_S1_1.data(), g_PERT_S1_1.size() },
  { g_PERT_S1_2.data(), g_PERT_S1_2.size() },
  { g_PERT_S1_3.data(), g_PERT_S1_3.size() },
  { g_PERT_S1_4.data(), g_PERT_S1_4.size() }
} };

It's quite explicit indeed ( I assume "array" is indeed "std::array"确实很明确(我假设“array”确实是“std::array”
https://en.cppreference.com/w/cpp/container/array , https://en.cppreference.com/w/cpp/container/array ,

and that you have a using namespace std somewhere...并且您在某处有一个 using namespace std ...

the constexpr g_PERT_S1 can only accept something build with constexpr. constexpr g_PERT_S1 只能接受用 constexpr 构建的东西。

so one of the g_PERT_S1_1 to g_PERT_S1_4 is not a constexpr.所以 g_PERT_S1_1 到 g_PERT_S1_4 之一不是 constexpr。

But as pointed in the comment, your example is not complete as we do not have those 4 definitions.但正如评论中所指出的,您的示例并不完整,因为我们没有这 4 个定义。

The size() is and data() member function of std::array are constexpr in C++17 but obviously they will return a constexpr only if the array is a constexpr. std::array 的 size() 和 data() 成员 function 在 C++17 中是 constexpr,但显然只有当数组是 constexpr 时它们才会返回 constexpr。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:Constexpr 变量必须由常量表达式初始化,但它是 - ERR: Constexpr variable must be initialized by a constant expression, but it is 错误! constexpr 变量必须由常量表达式 constexpr 初始化 - Error! constexpr variable must be initialized by a constant expression constexpr 错误:constexpr 变量“struct2Var”必须由常量表达式初始化 - error: constexpr variable 'struct2Var' 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? 使用从未打算在常量表达式中使用的 constexpr 变量是否有好处? - Are there benefits to using a constexpr variable that is never intended to be used in 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变量不是常数? - why is an uninitialized constexpr variable not constant? 如何用boost.hana解决“常量表达式中不允许读取非constexpr变量&#39;a&#39;”的问题 - How to solve the issue of "read of non-constexpr variable 'a' is not allowed in a constant expression" with boost.hana
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM