Generic way to cast int to enum in C++

前端 未结 8 2127
[愿得一人]
[愿得一人] 2020-12-04 11:26

Is there a generic way to cast int to enum in C++?

If int falls in range of an enum it should return

8条回答
  •  醉话见心
    2020-12-04 11:30

    If, as you describe, the values are in a database, why not write a code generator that reads this table and creates a .h and .cpp file with both the enum and a to_enum(int) function?

    Advantages:

    • Easy to add a to_string(my_enum) function.
    • Little maintenance required
    • Database and code are in synch

提交回复
热议问题