Re: Logical operator inside fold-expression: strange warning

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This also happens in much simpler cases:

template <bool... B>
bool foo () {
    return ((B && true) || ...);
}

int main () {
    foo<true, false, false, true> ();
}

I think it is a bug. It has existed (at least) since 6.3.1.

On 9/26/19 5:25 PM, Student FourK via gcc-help wrote:
> 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.





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux