Assuming that by “functor” you mean “function object” or “called object”, it does not seem to be what you want in the Library standard .
It is trivial to implement it yourself:
struct deferencer { template <typename T> decltype(auto) operator()(T&& x) const noexcept(noexcept(*x)) { return *x; } };
Note that your lambda does not do what you expect, since its implicit return type -> auto
, which makes a copy. The correct lambda is possible:
[](const auto& i) -> decltype(auto) { return *i; }
If you do not specify an explicit return type for the lambda, implicit will be auto
, which will always copy . It doesn’t matter if operator*
returns a link, because the lambda returns a copy (that is, the link returned by operator*
is then copied by the lambda operator return
).
struct A { A() = default; A(const A&) { puts("copy ctor\n"); } }; int main() { []{ return *(new A); }();
wandbox example
source share