Detect if a type is a std::tuple?

后端 未结 5 968
天涯浪人
天涯浪人 2020-12-31 05:01

Currently I have two functions :

template bool f(Type* x);
template bool f(std::tuple* x);
         


        
5条回答
  •  梦毁少年i
    2020-12-31 05:12

    Sure, using is_specialization_of (link taken and fixed from here):

    template::value>
    bool f(Type* x);
    

    The question is, however, do you really want that? Normally, if you need to know if a type is a tuple, you need special handling for tuples, and that usually has to do with its template arguments. As such, you might want to stick to your overloaded version.

    Edit: Since you mentioned you only need a small portion specialized, I recommend overloading but only for the small special part:

    template
    bool f(T* x){
      // common parts...
      f_special_part(x);
      // common parts...
    }
    

    with

    template
    void f_special_part(T* x){ /* general case */ }
    
    template
    void f_special_part(std::tuple* x){ /* special tuple case */ }
    

提交回复
热议问题