0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
qwa
  • 123
  • 10
  • Does static_cast construct an INT?. In the second approach a new INT is constructed. It's not a cast. – alirakiyan May 02 '21 at 10:23
  • `int(MyEnum::a);` is identical to `(int)MyEnum::a;` ie a c-style cast that if it can't do anything safe will use `reinterpret_cast` to get the job done probably resulting in Undefined Behaviour. `static_cast` will report an error and potentially save you from a typing (or other) mistake. – Richard Critten May 02 '21 at 10:24
  • 1
    You could also add a converter helper, `inline int to_int(MyEnum x) noexcept { return static_cast(x); }` – Eljay May 02 '21 at 12:44
  • 2
    @alirakiyan it is a cast see (2) here - https://en.cppreference.com/w/cpp/language/explicit_cast – Richard Critten May 02 '21 at 12:47
  • Thanks to all specifically @RichardCritten. I wanted to avoid the LONG and UGLY look of `static_cast<>()` but it seems this is by design and maybe i would be better off if I actually used static_cast ??? **Any comments???** I came to this conclusion after reading this [link](https://softwareengineering.stackexchange.com/questions/50442/c-style-casts-or-c-style-casts). – qwa May 02 '21 at 13:48
  • 2
    @Eljay Or you can define a unary `+` operator: `int operator +(MyEnum arg) { return static_cast(arg); }`. Then, if you're using it a lot, it's a lot less typing to just do stuff like: `int i = +MyEnum::b;`. – Adrian Mole May 02 '21 at 13:49
  • This [link2](https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used/332086#332086) mentions that `(Type)...` and `Type(...)` are both old c-style casts. BUT my compiler does not generatre warnings for the latter!!! Any way it looks i should avoid `int(MyEnum::a)` – qwa May 02 '21 at 13:53
  • @RichardCritten It's true that `T(expr)` is the same as `(T)expr` - but the "dangerous" cases of these apply to pointer and reference casts; if `T` is not a pointer or reference type, it will always act exactly like `static_cast`. So in my own coding style I'm fine with `T(expr)` as long as `T` is a keyword or possibly-qualified identifier, is not type-dependent, and is not an alias for a pointer or reference type. Calls for extra care inside a template, though. – aschepler May 02 '21 at 14:15
  • @AdrianMole • I use — *and really like* — the unary `+` as a converter in my own code. Many of the C++ devs I work with dislike that usage (although not as much as they dislike the use of `auto`), and declare it **forbidden** in the team's C++ coding guidelines. Alas. :-( – Eljay May 02 '21 at 15:01

0 Answers0