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