How to specialize a template function for enum, and specific type?

前端 未结 2 886
终归单人心
终归单人心 2021-01-14 10:40

I currently have a function:

template 
bool func(T &t, int x)
{
    // do stuff...
}

However I would like to have thr

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 11:18

    Use tag dispatching:

    namespace details {
      template
      bool func( T& t, int x, std::true_type /* is_enum */, std::false_type ) {
      }
      template
      bool func( T& t, int x, std::false_type, std::true_type /* unsigned char */ ) {
      }
      template
      bool func( T& t, int x, std::false_type, std::false_type ) {
        // neither
      }
    }
    template
    bool func( T& t, int x ) {
      return details::func( t, x, std::is_enum{}, std::is_same{} );
    }
    

提交回复
热议问题