is it correct to do this?
typedef unsigned int Index;
enum
{
InvalidIndex = (Index) -1
};
I have read that it is unsafe across platforms, but I have seen this in so many "professional" codes...
What you read was probably out of Fear, Uncertainty and Doubt. The author of whatever you read probably thought that (unsigned)-1
was underflowing and potentially causing chaos on systems where the bit representation doesn't happen to give you UINT_MAX
for your trouble.
However, the author is wrong, because the standard guarantees that unsigned values wrap-around when they reach the edge of their range. No matter what bit representations are involved, (unsigned)-1
is std::numeric_limits<unsigned>::max()
. Period.
I'm not sure what the benefit of it is here, though. You're going to get that large, maximum value.. If that is fine, I guess you're good to go.