开发者

template with enum fails to initialize its member with a POD template argument value

开发者 https://www.devze.com 2023-03-30 03:24 出处:网络
I am trying to create a lightweight template class having a static member initialized with the value provided as template argument( only enum are alowed ).

I am trying to create a lightweight template class having a static member initialized with the value provided as template argument( only enum are alowed ).

The code below doesn't build saying

   error: wrong number of template arguments (1, should be 2)
   error: provided for ‘template<field_id field_value, 
   inner_type_id innter_type_value>     struct node’
   error: template declaration of ‘field_id m_field_id’

The problem seems the first template argument. If I remove 'field_value' and 'inneer_type_value' still doesn't build saying mismatch error.

Can you help me ? Thanks AFG

   enum field_id{ FIELD_ID_1, FIELD_I_2  };
   enum inner_type_id{ INN_ID_1, INN_ID_2  };

    template< enum field_id  field_value
            , enum inner_type_id inner_type_value 
    >struct node{
            static field_id m_field_id;
    };

    template< 
            enum field_id  field_value
            ,enum inner_type_id inner_type_value 
    >
    enum field_id node<
            enum field_id field_value
            ,enum inner_type_id inner_type_value
    >::m_field_id=field_value;

    int main(){
           node<FIELD_ID_1,INN_ID_1> obj;
           assert( obj::m_field_value == FIELD_ID );
    }

I am also tyring another different approach, but still I don't get it

         template< typename T1, typename T2 >
         struct node;

         template<>
         struct node< enum field_id=FIELD_I开发者_开发百科D_1, enum inner_type_id=INN_ID_1>{
              static enum field_id m_field_id;
         };             
         note< 
               enum field_id=FIELD_ID_1
              ,enum inner_type_id=INN_ID_1
              >::m_field_id=FIELD_ID1;


The enum keyword cannot appear within template - just use the type name:

template<field_id field_value, inner_type_id inner_type_value> struct ...


In the end your code probably should look like:

enum field_id{ FIELD_ID_1, FIELD_I_2  };
enum inner_type_id{ INN_ID_1, INN_ID_2  };

template<field_id  field_value, inner_type_id inner_type_value>
//       ^ no enum keyword
struct node
{
  static field_id m_field_id;
};

template<field_id  field_value, inner_type_id inner_type_value>
field_id node<field_value, inner_type_value>::m_field_id = field_value;
//            ^ no enum and type

int main(){
  typedef node<FIELD_ID_1,INN_ID_1> my_type;
  assert( my_type::m_field_id == FIELD_ID_1 );
  //               ^ there's only m_field_id member
  my_type obj;
}


Edit: posted an answer that was wrong. Alternatively, you can make the static field const and define it inline:

template<field_id  field_value
        , inner_type_id inner_type_value 
>struct node{
        static const field_id m_field_id=field_value;
};


Why do you repeat the types of the template parameters in the instanciation of the static variable? I yould just write:

template <field_id field_value, inner_type_id inner_type_value>
field_id node <field_value, inner_type_value>::m_field_id = field_value;

Edit: this compile with gcc and works as expected.

0

精彩评论

暂无评论...
验证码 换一张
取 消