ฉันคิดว่าคุณสามารถใช้std :: underlying_typeเพื่อทราบประเภทพื้นฐานจากนั้นใช้ cast:
#include <type_traits> //for std::underlying_type
typedef std::underlying_type<my_fields>::type utype;
utype a = static_cast<utype>(my_fields::field);
ด้วยสิ่งนี้คุณไม่จำเป็นต้องถือว่าประเภทพื้นฐานหรือไม่ต้องพูดถึงในคำจำกัดความของสิ่งที่enum classคล้ายenum class my_fields : int { .... }กัน
คุณสามารถเขียนฟังก์ชันการแปลงทั่วไปที่ควรจะสามารถแปลงใด ๆ เป็นประเภทอินทิกรัลที่enum classอยู่ภายใต้:
template<typename E>
constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type 
{
   return static_cast<typename std::underlying_type<E>::type>(e);
}
จากนั้นใช้มัน:
auto value = to_integral(my_fields::field);
auto redValue = to_integral(Color::Red);//where Color is an enum class!
และเนื่องจากฟังก์ชั่นถูกประกาศให้เป็นconstexprคุณสามารถใช้ได้เมื่อต้องการนิพจน์คงที่:
int a[to_integral(my_fields::field)]; //declaring an array
std::array<int, to_integral(my_fields::field)> b; //better!
               
              
enumหากคุณต้องการแปลงประเภทพื้นฐานแล้วการใช้งาน