मैं 4 पूर्णांक स्थिरांक है:स्थिर रूप से जांचें कि दो अनुपात बराबर हैं?
const int a1 = 1024;
const int a2 = 768;
const int b1 = 640;
const int b2 = 480;
और मैं स्थिर जाँच करने के लिए वे एक ही अनुपात है चाहता हूँ। स्थिर रूप से जांचने के लिए, मैं का उपयोग कर रहा हूं, लेकिन यह अभिव्यक्तियों का समर्थन नहीं करता है।
मैं इस कोशिश की:
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
कैसे ऊपर लाइन ठीक करने के लिए?
पीएस मुझे सी ++ 0x फीचर्स और std :: static_assert तक पहुंच नहीं है, इसलिए मैं बूस्ट के स्थिर जोर का उपयोग कर रहा हूं।
+1: यह गणितीय रूप से बेहतर है। लेकिन सवाल अभी भी है, उसकी संकलन त्रुटि को कैसे हल करें। –
@Martijn मुझे लगता है कि ऊपर एकमात्र तरीका है। कोनराड की प्रतिक्रिया देखें –