Jim Fell Jim Fell - 1 year ago 104
C++ Question

What does an exclamation mark in array index do?

While perusing through my organization's source repository I came across this little gem:

RawParameterStorage[!ParameterWorkingIdx][ParameterDataOffset] = ...

Is this valid code? (It compiles) What does the exclamation mark here do?

An invert
operator might make sense, since it's commonly confused with the not
operator in boolean expressions. However, it doesn't seem to make logical sense to impose the not
operator on an array index. Any Thoughts?

Answer Source

!ParameterWorkingIdx Means ParameterWorkingIdx is different than 0, If it is, !ParameterWorkingIdx evaluates as true which might be implicitly converted to the indexer type (For example, 1 for integer indexer as in an array), otherwise, it evaluates as false.

  • If ParameterWorkingIdx == 0 then [!ParameterWorkingIdx] == [1].

  • If ParameterWorkingIdx != 0 then [!ParameterWorkingIdx] == [0].

It also depends on other stuff like:

  • The type of ParameterWorkingIdx.

  • overloading of ! operator by the type of ParameterWorkingIdx.

  • indexer overloading by the type of RawParameterStorage.

  • etc...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download