There is such code:
#include <assert.h>
void fun1()
{
int x;
}
int main()
{
assert(true
==true);
return 0;
}
void fun2()
{
int y;
}
There are two unused variables x
and y
which should be reported by a compiler when option -Wall
is passed.
Let's first run only preprocessor:
g++ main.cpp -Wall -E -o main2.cpp
The output:
a lot of stuff from assert.h
...
# 2 "main.cpp" 2
void fun1()
{
int x;
}
int main()
{
((true ==true) ? static_cast<void> (0) : __assert_fail ("true ==true",
"main.cpp"
# 10 "main.cpp" 3 4
,
11
# 10 "main.cpp" 3 4
, __PRETTY_FUNCTION__))
;
return 0;
}
void fun2()
{
int y;
}
Let's try to compile this thing:
$ g++ main2.cpp -Wall -c -o main2.o
main.cpp: In function ‘void fun1()’:
main.cpp:5:8: warning: unused variable ‘x’ [-Wunused-variable]
int x;
^
There is only a warning for the first unused variable but not for the second. It looks like that some warnings are disabled after the assert
macro.
But when I change assert
to be one-line statement:
int main()
{
assert(true==true);
return 0;
}
then these things:
# 10 "main.cpp" 3 4
are not generated and when I compile the preprocessed file then I get two warnings as expected for two unused variables.
I will get also two warnings when I remove 3 4
from these directives in preprocessed file:
# 10 "main.cpp" 3 4
to
# 10 "main.cpp"
Why the behaviour of the compiler (g++ 4.9.2) is so different when the condition inside assert
is one-line and multiline?