Как статически проверить равенство двух соотношений?

У меня есть 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.

5
задан Johan Råde 11 May 2012 в 09:18
поделиться