初始化时不能转换匿名枚举
cannot convert anonymous enum in initialization
为什么编译器在这里抱怨?
enum jit_ptx_type {f32=0,f64=1,u16=2,u32=3,u64=4,s16=5,s32=6,s64=7,u8=8,b16=9,b32=10,b64=11,pred=12 };
//
// MATCHING C TYPES TO PTX TYPES
//
template<class T> struct jit_type {};
template<> struct jit_type<float> { enum { value = jit_ptx_type::f32 }; };
template<> struct jit_type<double> { enum { value = jit_ptx_type::f64 }; };
template<> struct jit_type<int> { enum { value = jit_ptx_type::s32 }; };
template<> struct jit_type<bool> { enum { value = jit_ptx_type::pred }; };
后面的代码:
some_func( float val ) {
jit_ptx_type type = jit_type<float>::value; // compiler complains here
}
编译器信息:
error: cannot convert ‘jit_type<float>::<anonymous enum>’ to ‘jit_ptx_type’ in assignment
真奇怪!如果我把这些行放到一个单独的小示例文件中,它可以工作。
我倾向于让外部枚举变成一个有作用域的枚举:
enum class jit_ptx_type {
f32=0, //note the =x is unnecessary here
f64=1,
u16=2,
u32=3,
u64=4,
s16=5,
s32=6,
s64=7,
u8=8,
b16=9,
b32=10,
b64=11,
pred=12
};
现在您不会用所有这些标识符污染周围的作用域,并且您需要作用域限定符来访问值,而未限定作用域的枚举不允许这样做。接下来,在您的类中,只需使用静态常量成员:
template<> struct jit_type<float> {
static constexpr value = jit_ptx_type::f32;
};
相关文章:
- 错误:从"int"到枚举c++的转换无效
- 将作用域枚举转换为基础类型
- 枚举类的 C 样式强制转换到基础类型 char 的引用
- 用于 boost::p roperty_tree 的通用枚举转换器
- 在编译时将强类型枚举器转换为其基础类型?
- SWIG-将C++枚举转换为Python枚举
- 如何将C++代码中的枚举转换为 C
- 将枚举转换为_variant_t C++
- 将枚举转换为值的正确方法
- 通用 int -> 可以使用模板进行枚举转换吗?
- 在 C++ 中通过 int 将枚举转换为枚举
- 将枚举转换为字符串
- 超出范围的枚举转换能否生成基础类型之外的值
- 将已有的枚举转换为字符串
- 如何自动将强类型枚举转换为整型
- 如何将c++枚举转换为ctypes ?结构使用Python 2.7.2
- 将C++枚举转换为python int
- C++11 将枚举转换为整数
- 有没有一种简单的方法可以将C++枚举转换为字符串
- 应该如何将C位标志枚举转换为C++