1

コンパイラがここで文句を言うのはなぜですか?

  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

それは奇妙です!これらの行を別の小さなサンプル ファイルに入れると、機能します。

4

1 に答える 1

2

外側の列挙型をスコープ付き列挙型にするために行きます:

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; 
};
于 2013-04-04T21:55:30.037 に答える