我有 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 的静态断言的原因。