Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法在初始化中转换匿名枚举_C++_Templates_Enums_Traits - Fatal编程技术网

C++ 无法在初始化中转换匿名枚举

C++ 无法在初始化中转换匿名枚举,c++,templates,enums,traits,C++,Templates,Enums,Traits,为什么编译器在这里抱怨 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<flo

为什么编译器在这里抱怨

  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 }; };
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};
//
//将C类型与PTX类型匹配
//
模板结构jit_类型{};
模板结构jit_type{enum{value=jit_ptx_type::f32};};
模板结构jit_类型{enum{value=jit_ptx_类型::f64};};
模板结构jit_type{enum{value=jit_ptx_type::s32};};
模板结构jit_type{enum{value=jit_ptx_type::pred};};
代码后面部分:

  some_func( float val ) {
    jit_ptx_type type = jit_type<float>::value;   // compiler complains here
  }
some_func(float val){
jit_ptx_type=jit_type::value;//编译器在这里抱怨
}
编译器消息:

error: cannot convert ‘jit_type<float>::<anonymous enum>’ to ‘jit_ptx_type’ in assignment
错误:无法将分配中的'jit\u type::'转换为'jit\u ptx\u type'

真奇怪!如果我将这些行放在一个单独的小示例文件中,它就会工作。

我会将外部枚举放入一个作用域枚举中:

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; 
};
模板结构jit_类型{
静态constexpr value=jit_ptx_type::f32;
};

非范围枚举不使用限定。我会使用
静态constexpr int value=f32
,或者使用
enum类来代替
jit\u ptx\u type::f32
。乍一看,它似乎与错误无关,尽管它的语法无效。仍然是相同的编译器消息。它是否与静态常量成员而不是具有一个值的匿名枚举一起工作?是的,它与
static constepr jit_ptx_type value=f32
那么它一定是附加的
枚举
。但是,在一个裁剪示例中,它与
enum
一起使用。这就是奇怪的地方。不过,非常感谢!你认为这里贴的第一行一般可以吗?或者我应该像你说的那样使用枚举类?