gpt4 book ai didi

c++ - 为什么我可以使用函数为常量赋值?

转载 作者:行者123 更新时间:2023-11-28 06:32:30 26 4
gpt4 key购买 nike

当 PI 是常量并在编译时确定而 4*atan(1) 仅在运行时确定时,为什么 C++ 中允许以下代码。

int main(){
const double PI = 4*atan(1);
std::cout << PI << std::endl;
return 0;
}

我哪里做错了?

最佳答案

您可以使用 not-constexpr 函数来初始化该常量,因为它不是编译时常量,而只是一个const-限定变量。

不幸的是,大多数math.h函数不能是constexpr,因为它们可以设置errno
coliru 错误:http://coliru.stacked-crooked.com/a/07a87519a0cdab07

关于c++ - 为什么我可以使用函数为常量赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27296077/

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