我有4个整数常量:
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的静态断言。