且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测类型是否为std :: tuple?

更新时间:2023-12-01 07:54:10

当然,使用 is_specialization_of (从此处):

template<typename Type, bool IsTuple = is_specialization_of<Type, std::tuple>::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.

编辑:由于您提到您只需要一小部分专业版本,因此我建议您重载但仅适用于小的特殊部分:

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

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



with

template<class T>
void f_special_part(T* x){ /* general case */ }

template<class... Args>
void f_special_part(std::tuple<Args...>* x){ /* special tuple case */ }