const-ness as template argument

后端 未结 3 1052
遇见更好的自我
遇见更好的自我 2021-02-07 23:59

I have two structs:

  // ----- non-const -----
  struct arg_adapter
  {
      EArgType type;  // fmtA, fmtB, ...

      union
      {
        TypeA * valueA;
            


        
3条回答
  •  甜味超标
    2021-02-08 00:53

    You can make it accept a metafunction and you can apply any transformation you like

    template class F>
    struct arg_adapter
    {
        EArgType type;  // fmtA, fmtB, ...
    
        union
        {
          typename F::type * valueA;
          typename F::type * valueB;
          // ... more types
        };
    
        arg_adapter(typename F::type & value) : type(fmtA), valueA(&value) {}
        arg_adapter(typename F::type & value) : type(fmtB), valueB(&value) {}
        // ...
    };
    
    typename arg_adapter const_adapter;
    typename arg_adapter nonconst_adapter;
    

    Or accept a metafunction class to get more flexibility (including the ability to make F have default arguments not known to your arg_adapter and such.

    template
    struct arg_adapter
    {
        EArgType type;  // fmtA, fmtB, ...
    
        union
        {
          typename apply::type * valueA;
          typename apply::type * valueB;
          // ... more types
        };
    
        arg_adapter(typename apply::type & value) : type(fmtA), valueA(&value) {}
        arg_adapter(typename apply::type & value) : type(fmtB), valueB(&value) {}
        // ...
    };
    
    typename arg_adapter< lambda< boost::add_const<_> >::type > const_adapter;
    typename arg_adapter< lambda< boost::mpl::identity<_> >::type > nonconst_adapter;
    

提交回复
热议问题