It's been brought to my attention by the answer to this question that lambdas I thought were returning by reference are copying. If we define this for example:
struct A {
A() = default;
A(const A&) { cout << "copy\n"; }
};
None of this code calls the copy constructor:
A a;
const A* pa = &a;
const A& ra = *pa;
But this code calls the copy constructor on return:
[](const A* pa){ return *pa; }(pa);
I don't get it. Why is it returning by copy? Or more generally I guess I should ask: "How does a lambda decide how to return?"