Please consider the following code:
class cls
{
};
__attribute__((warn_unused_result))
cls fn1()
{
return cls();
}
__attribute__ ((warn_unused_result))
int fn2()
{
return 1;
}
int main()
{
auto x = fn1();
(void)x;
auto y = fn2();
(void)y;
return 0;
}
When I compile this with -Wall -Wextra
with gcc 5.4.0, I get a warning:
warning: ignoring return value of ‘cls fn1()’,
declared with attribute warn_unused_result [-Wunused-result]
Can somebody tell me what's wrong?
Why this warning appears for fn1
despite the x
variable, and why it does NOT appear for the fn2
?
Update 1: With a little help, I tried this with g++ 4.8.3 - the same result. Even more - g++ 3.3.6 produces two warnings - for fn1
and fn2
.
What "fixes" the warning is...... guess what - adding a data member or a virtual member function in the class (a non-virtual function does not help). Sounds like a compiler bug to me.
Update 2: Actually adding a member or a virtual function disables the warning completely, so just calling fn1();
does NOT produce the warning anymore.