gpt4 book ai didi

c++ - "constinit"和 "constexpr"之间的真正区别是什么?

转载 作者:行者123 更新时间:2023-12-01 14:39:11 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





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() {}

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

constinit之间的真正区别是什么?和 constexpr ?

更新:

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

最佳答案

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

c2 = 2; 

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com