To convert a strongly-typed enum to int we can use:
enum class MyEnum { a, b };
int x = static_cast<int>(MyEnum::a);
what if i use the following line, which is shorter:
int x = int(MyEnum::a);
My motivation:
I need to cast my scoped enums and my compiler (Qt creator's default i.e. mingw) generates warnings when i use old c-style casts e.g. (int)MyEnum::a
, which I am looking to avoid. Obviously static_cast<>()
is LONG and UGLY. So one might prefer int(MyEnum::a)
which generates NO warnings in my compiler
Questions:
1- Is it just right to do it this way?
2- What are the pros and cons of the 2 approaches?