解决PCLint warning问题
#define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) { \
double expectedVal=expected; \
double actualVal=actual; \
double deltaVal=delta; \
double diffVal=fabs((expectedVal)-(actualVal)); \
if((diffVal) <(deltaVal)){ \
PRINT_OUT( "Values - Expected: " #expected " - Actual: " #actual " - Delta: " #delta); \
}else { \
CPPUNIT_NS::assertDoubleEquals( (expectedVal), \
(actualVal), \
(deltaVal), \
CPPUNIT_SOURCELINE(), \
" " ); \
} \
}
请问这段代码有什么问题? 为什么用PCLint检查出来有
Warning 665: (Warning -- Unparenthesized parameter 1 in macro 'CPPUNIT_ASSERT_DOUBLES_EQUAL ' is passed an expression)
的警告?
以下是关于此警告的解释:
665 Unparenthesized parameter Integer in macro 'Symbol ' is passed an expression
-- An expression was passed to a macro parameter that was not parenthesized. For example:
#define mult(a,b) (a*b)
... mult( 100, 4 + 10 )
Here the programmer is beguiled into thinking that the 4+10 is taken as a quantity to be multiplied
by 100 but instead results in: 100*4+10, which is quite different. The recommended remedy
([22 section 20.4]) is to parenthesize such parameters as in:
#define mult(a,b) ((a)*(b))
The message is not arbitrarily given for any unparenthesized parameter but only when the actual
macro argument sufficiently resembles an expression and the expression involves binary operators.
The priority of the operator is not considered except that it must have lower priority than
the unary operators. The message is not issued at the point of macro definition because it may
not be appropriate to parenthesize the parameter. For example, the following macro expects that
an operator will be passed as argument. It would be an error to enclose op in parentheses.
#define check(x,op,y) if( ((x) op (y)) == 0 ) print( ... )
我的代码有此类问题吗?
[解决办法]
double expectedVal=(expected);\
double actualVal=(actual);\
double deltaVal=(delta);
兄弟啊,基本功不足,可照猫画虎都没画对啊。
