Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
enum class test : bool { yes=true, no=false }; template< bool ok > class A { }; int main(){ A<test::yes> a; }
为什么编译失败?(g++ 4.7) 由于 C++11 枚举是强类型的,所以我们应该能够使用 bool 枚举作为模板类型的 bool 参数吗?
强类型枚举意味着您只能隐式地比较和分配同一枚举类中的值。解决方案是使用非强类型枚举示例:
enum test : bool { yes = true, no = false }; bool x = test::yes; // ok
或者按照 Tom Knapen 的建议:显式转换枚举
enum class test : bool { yes = true, no = false }; bool x = static_cast<bool>(test::yes); // ok