Jim Fell Jim Fell - 2 months ago 7
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

!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...

Comments