This question already has an answer here:
What is `constinit` in C++20?

(1 个回答)


去年关闭。



constexpr int f() { return 0; }
int g() { return 0; }

constexpr auto c1 = f(); // OK
constinit auto c2 = f(); // OK

constexpr auto d1 = g(); // ill-formed
constinit auto d2 = g(); // ill-formed

int main() {}

如上面的代码所示,我找不到 constinitconstexpr 之间的任何区别。

constinitconstexpr 之间的真正区别是什么?

更新:

相关的 What is constinit in C++20? 没有明确说明 constinitconstexpr 之间的区别。

最佳答案

constinit 变量是常量初始化的,但它不能用于常量表达式,甚至不能自动常量。您的 main 可以合法地包含这一行

c2 = 2;

是的,初始化后可以修改。

关于c++ - "constinit"和 "constexpr"之间的真正区别是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62444495/

10-14 09:09