变换元组类型 [英] Transform tuple type

查看:163
本文介绍了变换元组类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要一个覆盖这种类型的元函数:

  std :: tuple< T0,T1,...,TN& 

进入:

  std :: tuple< 
std :: function< T0(std :: tuple< T0,T1,...>,std :: tuple< T0,T1,...>),
std: :function< T1(std :: tuple< T0,T1,...>,std :: tuple< T0,T1,...>),
...,
std :: function< TN(...)>
>

看起来像这样可以通过 transform ,但我想有一个元组类型,而不是一个类型的向量。 (它不必使用MPL实际上,但我想它会更短?)



背景:目前我使用完全通用的类型,并依赖于所有地狱破如果使用错误,但我想计算 TupleOfFunctions 以获得正确的错误。

  template< class TupleOfValues,class TupleOfFunctions> 
void f(TupleOfValues v,TupleOfFunctions fun)


解决方案

p>以下内容?

 模板< typename T> struct transform; 
template< typename ... T>
struct transform< std :: tuple< T ...>> {
typedef std :: tuple< std :: function< T,std :: tuple< T ...> ;, std :: tuple< T ...>> ...&类型;
};


So I'm new to boost MPL, and I don't know how to use it with standard types.

I want a metafunction that coverts this type:

std::tuple<T0, T1, ..., TN>

Into this:

std::tuple<
  std::function<T0(std::tuple<T0, T1, ...>, std::tuple<T0, T1, ...>)>,
  std::function<T1(std::tuple<T0, T1, ...>, std::tuple<T0, T1, ...>)>,
  ...,
  std::function<TN(...)>
>

and it seems like this could be done with transform, but I want to have a tuple-type, not a vector of types. (It doesn't have to use MPL actually, but I guess it would be shorter?)

Background: currently I use totally generic types and rely on all hell breaking loose if used wrong, but I want to calculate the TupleOfFunctions to get a proper error.

template<class TupleOfValues, class TupleOfFunctions>
void f(TupleOfValues v, TupleOfFunctions fun)

解决方案

How about the following?

template<typename T> struct transform;
template<typename ...T>
struct transform<std::tuple<T...>> {
  typedef std::tuple<std::function<T, std::tuple<T...>, std::tuple<T...>>...> type;
};

这篇关于变换元组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆