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…
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What you read was probably out of Fear, Uncertainty and Doubt. The author of whatever you read probably thought that
(unsigned)-1was underflowing and potentially causing chaos on systems where the bit representation doesn’t happen to give youUINT_MAXfor 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)-1isstd::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.