left-hand operand of comma has no effect?

后端 未结 3 938
北恋
北恋 2021-01-05 08:26

I\'m having some trouble with this warning message, it is implemented within a template container class

int k = 0, l = 0;
    for ( k =(index+1), l=0; k <         


        
相关标签:
3条回答
  • 2021-01-05 09:04

    The expression k < sizeC, l < (sizeC-index) only returns the result of the right-hand test. Use && to combine tests:

    k < sizeC && l < (sizeC-index)
    
    0 讨论(0)
  • 2021-01-05 09:21

    Change to:

    for ( k =(index+1), l=0; k < sizeC && l < (sizeC-index); k++,l++){
    

    When you have a comma expression is evaluated the rightmost argument is returned so your:

     k < sizeC, l < (sizeC-index)
    

    expression evaluates to:

     l < (sizeC-index)
    

    and thus misses

     k < sizeC
    

    use the && to combine the conditions instead.

    0 讨论(0)
  • 2021-01-05 09:24

    The comma expression a,b,c,d,e is similar to

    {
      a;
      b;
      c;
      d;
      return e;
    }
    

    therefore, k<sizeC, l<(sizeC - index) will only return l < (sizeC - index).

    To combine conditionals, use && or ||.

    k < sizeC && l < (sizeC-index)  // both must satisfy
    k < sizeC || l < (sizeC-index)  // either one is fine.
    
    0 讨论(0)
提交回复
热议问题