1

Even setting the size of an enum is not enough to be able to assign it to a variable of the same size (http://coliru.stacked-crooked.com/a/edd287393f172abd):

#include <iostream>
#include <cstdint>

enum class Command : uint8_t
{
    CMD1    = 0x48,
    CMD2    = 0x97,
    CMD3    = 0x98,
    CMD4    = 0x92
};

int main()
{
    uint8_t test = Command::MACHINE_INFO;    
}

Is there a way I could use the enum to initialize my uint8_t variable without casting it?

cigien
  • 57,834
  • 11
  • 73
  • 112
vegalock
  • 51
  • 7
  • You can define a unary `+` operator as a `friend` of your `enum class` then use `uint8_t test = +Command::MACHINE_INFO;`. I'm sure I got that trick from Stack Overflow but I can't find the post ... – Adrian Mole Aug 11 '21 at 16:15

0 Answers0