Transform tuple type

自作多情 提交于 2019-12-19 10:15:20

问题


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)

回答1:


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;
};


来源:https://stackoverflow.com/questions/9123712/transform-tuple-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!