0

I am using an (external) function that receives two output-iterators as arguments and fills them with something. The usage is like this

std::vector<X> vals_X;
std::vector<Y> vals_Y;

f(std::back_inserter(vals_X),std::back_inserter(vals_Y));

In my application, I only need the vector vals_X, so I just fill vals_Y and ignore it. That is inefficient, as the vectors can be quite large. I would like to pass an iterator that "pipes to /dev/null", so it just forgets all values it receives right away.

I was surprised that I could not find a standard solution for that. I did find std::ignore, but I cannot pass it as second argument as it does not provide ++, resulting in a compiler error.

mike
  • 75
  • 2
  • 1
    How external is the function? Is it from a third party? Sometimes the real solution is a change request instead of trying to work around the problem, because in the end you will just end up with a codebase full of junk. – Pepijn Kramer Jun 09 '23 at 15:48
  • I agree, but here, it is really a function where I have no influence on the interface – mike Jun 09 '23 at 15:57
  • that happens too. Sometimes I make a function (in an abstract baseclass) with the signature I would like it to have use that from the rest of my codebase. And then hide the nitty gritty workarounds in the implementation. – Pepijn Kramer Jun 09 '23 at 16:02
  • `std::ranges::views::repeat(std::ignore).begin()` is an interestingly direct solution to this. Though it requires C++23 – Artyer Jun 09 '23 at 16:06
  • Quick-and-dirty, just take `back_insert_iterator` and `back_inserter`, and make a do-nothing `back_ignore_iterator` and `back_ignorer` and *voila!* – Eljay Jun 09 '23 at 16:56

0 Answers0