I'm reviewing a lot of code where I need to ensure there are no static_cast (or any cast) calls on variables that could be out of range of the enum class that is being cast to. Ideally I'd be able to receive a warning or have some way to detect code that is casting from, for example, an int when the enum class in question has an underlying type of unsigned char.
enum class some_enum_class : unsigned char
{
SomeVal,
};
int BadVal = 1000;
auto EnumVar = static_cast<some_enum_class>(BadVal); //Trigger Warning!
Is there any way to accomplish this? (hopefully in MSVC?)
Doing your own casting function can help you, you can wrap the
const_castfunction using concepts and constraints ofc++20which specifies the requirements on template arguments. And for example clang tidy recognize it and throw a warning before any compilation.