Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add projections to tuple operations #70

Open
marcorubini opened this issue Dec 18, 2021 · 0 comments
Open

add projections to tuple operations #70

marcorubini opened this issue Dec 18, 2021 · 0 comments

Comments

@marcorubini
Copy link

Assume we want to invoke tuple_for_each or tuple_apply on the keys or values of a map. We can encode our projection inside the function object passed as argument, but it would be nicer to pass a unary function as optional third argument.

Consider this example

template<class... Pairs>
constexpr void reset_values1 (std::tuple<Pairs...>& map)
{
  mp11::tuple_for_each (map, [] (auto& p) {
    p.second = 0;
  });
}

template<class... Pairs>
constexpr void reset_values2 (std::tuple<Pairs...>& map)
{
    auto set_zero = [] (auto& x) {
      x = 0;
    };
    auto second = [] (auto& x) -> auto& {
      return x.second;
    };
    mp11::tuple_for_each (map, set_zero, second);
}

The second implementation is more verbose because we had to write some function objects, but it enables easier composition.
mp11 doesn't have useful function objects to compose with this feature, but higher order function libraries do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant