Logical operator inside fold-expression: strange warning
Student FourK via gcc-help
gcc-help@gcc.gnu.org
Thu Sep 26 15:25:00 GMT 2019
Hello All,
I am using a nontrivial fold-expression, where additional logical expression is meant to evaluate only part of the parameter pack (not all of them).This is used to execute a part of a sequence of operations on statically defined types until any of the operations fails returning failure exit code.Here is the code:
/* SOURCE BEGIN */
#include <iostream>#include <functional>
template <class... Ts>
int Returns(Ts&&... args)
{
int i = 0;
int tmp;
(((tmp = args()) && (i = tmp)) || ...);
return i;
}
int main(){ std::cout << Returns([](){return 0;}, [](){return 9;}, [](){return 3;}) << std::endl;
}
/* SOURCE END */
This code prints "9" since the second lambda in Returns arguments is the first function that returns non-zero value.
The issue is the warning that GCC (tried with 9.1) emits when compiling the code:
main.cpp: In instantiation of 'int Returns(Ts&& ...) [with Ts = {main()::<lambda()>, main()::<lambda()>, main()::<lambda()>}]':main.cpp:13:75: required from heremain.cpp:8:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses] 8 | (((tmp = args()) && (i = tmp)) || ...); | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~main.cpp:8:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
Putting additional parentheses around the left side of "||" does not help.
So, I wonder if this is a bug and a bug report should be issued.
More information about the Gcc-help
mailing list