mardi 4 janvier 2022

strongly typed enum from int . C++11

We are recently converting all enum to class enum .

enum class Launches : std::uint32_t {
  D_FROM_FILE=0,
  D_FROM_FDISK,
  D_FROM_RDDISK,
  D_FROM_USB,
  D_FROM_UI,
  D_FROM_NET,
  D_FROM_DEFAULT
};

Launches  launches ;

I need to assign launches values based on int that i received from command line

This is what I am doing after conversion to strongly typed enum if(receivedInt == 1) launches = D_FROM_FDISK ;

I want to avoid and do like launches = receivedInt .

We fail to compile as this is what strongly typed int are supposed to do .

I want to avoid if and switch cases on receivedInt. launches = (Launches)receivedInt ; // this compiles .. but i am hesitant on this or on using static_cast ... whats the proper way ?

using static_cast etc ? What benefit will remain on using strongly types enums then

Aucun commentaire:

Enregistrer un commentaire