MSVC std::isfinite 的编译问题
您好,以下代码无法在 MSCV 中编译;它给出错误
(9): error C3615: constexpr function 'Test' cannot result in a constant expression due to the use of std::isfinite!?constexpr int Test(double value)
{
if (!std::isfinite((double)value))
{
return 1;
}
return 0;
}
//}
gcc 没问题,如何修复它?
Hi the following codes won't compile in MSCV; it gives error
(9): error C3615: constexpr function 'Test' cannot result in a constant expression due to the use of std::isfinite!?
constexpr int Test(double value)
{
if (!std::isfinite((double)value))
{
return 1;
}
return 0;
}
//}
gcc is fine, how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 C++23 之前,
std::isfinite
未标记为constexpr
。显然 gcc 的标准库已经实现了这个功能,而 MSVC 还没有。确保在使用 MSVC 进行编译时,使用
/std:c++latest
请求最新版本的标准。但我猜他们只是还没有做出改变。同时,您可以编写自己的
isfinite
实现,即constexpr
。我认为关于 IEEE 浮点的维基百科页面有足够的细节来提出适当的测试条件。我认为无穷大和 NaN 值将指数字段的所有位设置为 1。std::isfinite
isn't marked asconstexpr
until C++23. Apparently gcc's standard library has already implemented this feature and MSVC's hasn't yet.Make sure, when you compile with MSVC, to ask for the latest version of the standard with
/std:c++latest
. But I'm guessing they just haven't made that change yet.In the meantime, you could write your own implementation of
isfinite
that isconstexpr
. I think the Wikipedia page on IEEE floating point has enough detail to come up with the appropriate test condition. I think the infinities and NaN values have all bits of the exponent field set to 1.