#2922. C++-编译-warning: suggest parentheses around assignment used as...
C++-编译-warning: suggest parentheses around assignment used as...
Background
Description
2.warning: suggest parentheses around assignment used as truth value [-Wparentheses]
#include <iostream>
using namespace std;
bool test_bool()
{
return false;
}
int main(int argc, char *argv[])
{
(void)argc,(void)argv;
bool ret;
if( ret = test_bool() ){
cout << "Hello World!" << endl;
}
return 0;
}
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if( ret = test_bool() ){
^
if( ret = test_bool() )其实做了两件事。第一将test_bool()的结果赋值给ret,第二然后判断ret的bool值。
之所以会有警告,因为程序员写代码的时候,有时候没注意,会出现想表达“==”,却写成“=”的情况。
这种警告加一个括号就好了。 if( (ret = test_bool()) )
Format
Input
Output
Samples
Limitation
1s, 1024KiB for each test case.