pascal
pascal

Reputation: 2713

Transform tuple type

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)

Upvotes: 2

Views: 1253

Answers (1)

keveman
keveman

Reputation: 8487

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

Upvotes: 5

Related Questions