lundi 4 septembre 2017

How do I extract and execute a target from a std::function

I have something equal to this:

auto dummy(std::function<int(int)> ifunc) {
  auto vals = std::vector<int>(1000000);
  for(auto& v: vals) {
    v = ifunc(v);
  }
}

Assuming the std::function does not bind to a lambda with state (althrough it might be bound to a lambda without state), what is the correct way of extracting the underlaying function pointer and exectuing it instead of the std::function?

This is my attempt, but it crashes regardles if I dereference the target or not.

auto dummy(std::function<int(int)> ifunc) {
  auto vals = std::vector<int>(1000000);
  auto&& target = *ifunc.target<int(int)>();
  for(auto& v: vals) {
    v = target(v);
  }
}

Aucun commentaire:

Enregistrer un commentaire