gpt4 book ai didi

c++ - 如何静态检查两个比率是否相等?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:32 25 4
gpt4 key购买 nike

我有 4 个 int 常量:

const int a1 = 1024;
const int a2 = 768;
const int b1 = 640;
const int b2 = 480;

我想静态检查它们是否具有相同的比率。为了进行静态检查,我使用了 BOOST_STATIC_ASSERT,但它不支持表达式。

我试过这个:

BOOST_STATIC_ASSERT( 1e-5 > std::abs( (double)a1 / (double)a2 - (double)b1 / (double)b2 ) );

但这会产生下一个编译错误:

error: floating-point literal cannot appear in a constant-expression
error: 'std::abs' cannot appear in a constant-expression
error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
error: a function call cannot appear in a constant-expression
error: template argument 1 is invalid

如何修正上面那行才能使编译通过?

PS 我无权访问 c++0x 功能和 std::static_assert,这就是我使用 boost 的静态断言的原因。

最佳答案

BOOST_STATIC_ASSERT(a1 * b2 == a2 * b1);

关于c++ - 如何静态检查两个比率是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6279410/

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