I would like to know if its possible without having to define an extra class, if a lambda can be adapted to act as a sink.
For example we currently can do the following:
std::vector<int> ilst;
std::copy(ilst.begin(),ilst.end(),std::ostream_iterator<int>(std::cout,"\n"));
What if something like the following could be possible? obviously the following wont
compile atm:
std::copy(ilst.begin(),ilst.end(),
[](const int& i)
{
std::cout << i << "\n";
});
I’ve been able to get around this problem, by defining a function object that implements dereference and function operators and takes a lambda as a predicate.
However I was wondering if there is some kind of C++ voodoo that will allow for the above without the need for an extra intermediary class to be provided?
You cannot do this without an adapter object.
std::copytakes an output iterator which conforms to the standard library output iterator requirements. A lambda function is an object that has anoperator()that takes certain arguments and returns a value. These are two different kinds of things.If you have an interface that takes Y, but you have a X, the only way to reconcile this is to introduce a Z that converts X into Y.
And since X and Y are objects, Z must therefore be an object that provides the Y interface, but internally converts it into an X. Z is commonly called an adapter object.
There is no alternative “C++ voodoo” that’s going to change this. There is no “other solution”. You must use some kind of adapter. Whether it’s a temporary of a class type or a function that returns an instance of a class, this can only be resolved with an adapter object.
Applied to this particular situation – X is a lambda, Y is an output iterator, and Z is a function_output_iterator: