实施一个警官断言,当不在警官上下文中时,优雅地回到了断言吗?

发布于 2025-01-26 02:14:10 字数 1557 浏览 2 评论 0 原文

相关:

我想拥有一个主体,以便尽可能 static_assert ,但是当 static_assert 不可能。我的感觉是 std :: is_constant_evaLED()应该通过允许constexper执行使用与运行时执行不同的实现来帮助解决此问题,但是我尝试过的一切都失败了:

#define ASSERT0(cond) \
if (std::is_constant_evaluated()) { \
    static_assert((cond)); \
} else { \
    assert((cond)); \
}

#define ASSERT1(cond) \
if constexpr (std::is_constant_evaluated()) { \
    static_assert((cond)); \
} else { \
    assert((cond)); \
}

#define ASSERT2(cond) \
if consteval { \
    static_assert((cond)); \
} else { \
    assert((cond)); \
}

https://godbolt.org/z/kzwfxmdmdmd

基本上,如 @bop指出,正在在constexpr上下文中评估一个函数,其参数仍然不是积分常数()。

我是 std :: is_constant_evaled()的新手。我缺少什么吗?是否应该能够在 static_assert 断言之间切换?

Related:

I want to have an assert macro that when possible is a static_assert, but falls back to assert when static_assert isn't possible. My sense is that std::is_constant_evaluated() should help with this, by being intended to allow constexper execution to use a different implementation than runtime execution, but everything I've tried like that fails:

#define ASSERT0(cond) \
if (std::is_constant_evaluated()) { \
    static_assert((cond)); \
} else { \
    assert((cond)); \
}

#define ASSERT1(cond) \
if constexpr (std::is_constant_evaluated()) { \
    static_assert((cond)); \
} else { \
    assert((cond)); \
}

#define ASSERT2(cond) \
if consteval { \
    static_assert((cond)); \
} else { \
    assert((cond)); \
}

https://godbolt.org/z/KzWfxMdMd

Fundamentally, as @BoP points out, the challenge is: when a function is being evaluated in a constexpr context, its arguments still aren't integral constants (https://godbolt.org/z/co93nc59h).

I'm new to std::is_constant_evaluated(). Is there something I'm missing? Should it be able to be used to switch between static_assert and assert?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文