Questions tagged «strongly-typed-enum»

11
如何自动将强类型的枚举转换为int?
#include <iostream> struct a { enum LOCAL_A { A1, A2 }; }; enum class b { B1, B2 }; int foo(int input) { return input; } int main(void) { std::cout << foo(a::A1) << std::endl; std::cout << foo(static_cast<int>(b::B2)) << std::endl; } 这a::LOCAL_A是强类型枚举试图实现的目标,但是有一个小的区别:普通枚举可以转换为整数类型,而强类型枚举不能在没有强制转换的情况下做到。 因此,有没有一种方法可以将强类型的枚举值转换为整数类型而无需强制转换?如果是,怎么办?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.