I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist.
So given the code:
const auto vals = { 0, 1, 2, 3 };
vector<const int*> test(size(vals), nullptr);
iota(begin(test), end(test), data(vals));
transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; });
I was hoping that there was a functor that I could use instead of the lambda. Does such a thing exist, or do I need to just use the lambda?
Assuming that by "functor" you mean "function object" or "callable object", there doesn't seem to be what you desire in the Standard Library.
It is trivial to implement it yourself:
Note that your lambda doesn't do what you expect, as its implicit return type is
-> auto
, which makes a copy. One possible correct lambda is:If you don't specify an explicit trailing return type for a lambda, the implicit one will be
auto
which is always a copy. It doesn't matter ifoperator*
returns a reference, since the lambda returns a copy (i.e. the reference returned byoperator*
is then copied by the lambda'sreturn
statement).wandbox example