ritter
ritter

Reputation: 7699

cannot convert anonymous enum in initialization

Why does the compiler complain here?

  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 }; };

later in the code:

  some_func( float val ) {
    jit_ptx_type type = jit_type<float>::value;   // compiler complains here
  }

Compiler message:

error: cannot convert ‘jit_type<float>::<anonymous enum>’ to ‘jit_ptx_type’ in assignment

It's strange! If I put these lines into a separate small example file it works.

Upvotes: 1

Views: 2090

Answers (1)

Qaz
Qaz

Reputation: 61910

I'd go for making the outer enum into a scoped enum:

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 
};

Now you don't pollute the surrounding scope with all of those identifiers and you need scope qualifiers to access the values, whereas unscoped enums disallow that. Next, in your class, just use a static constant member:

template<> struct jit_type<float> { 
    static constexpr value = jit_ptx_type::f32; 
};

Upvotes: 2

Related Questions