Safe to cast uint8 to enum and vice versa?
// get a random tile
uint8 n = FMath::RandRange(1, (uint8)ETileType::EEnd - 1);
Is this safe? Is there a better way?
I would say this is ok assuming you don't want to get the ENone result (because enums start from 0, not 1).
Your enum is backed by a uint8 here, so no problems with too big numbers. You use EEnd as the max range which is good because you can safely add new enumeration.
Maybe I would use static_cast here, because it's C++ after all and later on you can easily search for casts in your project. Also maybe explicitly state what's the start of the range. Something like that:
But this is a more-or-less cosmetic change, so you decide :)
answered Feb 15 '16 at 08:33 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here