MSVC std::isfinite 的编译问题

发布于 2025-01-16 23:57:28 字数 329 浏览 2 评论 0原文

您好,以下代码无法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天荒地未老 2025-01-23 23:57:28

在 C++23 之前,std::isfinite 未标记为 constexpr。显然 gcc 的标准库已经实现了这个功能,而 MSVC 还没有。

确保在使用 MSVC 进行编译时,使用 /std:c++latest 请求最新版本的标准。但我猜他们只是还没有做出改变。

同时,您可以编写自己的 isfinite 实现,即 constexpr。我认为关于 IEEE 浮点的维基百科页面有足够的细节来提出适当的测试条件。我认为无穷大和 NaN 值将指数字段的所有位设置为 1。

std::isfinite isn't marked as constexpr 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 is constexpr. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文