逗号的左手操作数没有效果?

sil*_*ent 7 c++ gcc templates warnings

我在使用此警告消息时遇到了一些问题,它是在模板容器类中实现的

int k = 0, l = 0;
    for ( k =(index+1), l=0; k < sizeC, l < (sizeC-index); k++,l++){
        elements[k] = arryCpy[l];
    }
    delete[] arryCpy;
Run Code Online (Sandbox Code Playgroud)

这是我得到的警告

cont.h: In member function `void Container<T>::insert(T, int)':
cont.h:99: warning: left-hand operand of comma has no effect
cont.h: In member function `void Container<T>::insert(T, int) [with T = double]':
a5testing.cpp:21:   instantiated from here
cont.h:99: warning: left-hand operand of comma has no effect
cont.h: In member function `void Container<T>::insert(T, int) [with T = std::string]':
a5testing.cpp:28:   instantiated from here
cont.h:99: warning: left-hand operand of comma has no effect
>Exit code: 0
Run Code Online (Sandbox Code Playgroud)

ken*_*ytm 16

逗号表达式a,b,c,d,e类似于

{
  a;
  b;
  c;
  d;
  return e;
}
Run Code Online (Sandbox Code Playgroud)

因此,k<sizeC, l<(sizeC - index)只会返回l < (sizeC - index).

要结合条件,请使用&&||.

k < sizeC && l < (sizeC-index)  // both must satisfy
k < sizeC || l < (sizeC-index)  // either one is fine.
Run Code Online (Sandbox Code Playgroud)