The following compiles. But is there ever any sort of dangling reference issue?
class Foo {
Foo(std::function fn) { /* etc
But is there ever any sort of dangling reference issue?
That depends entirely on what you're doing with Foo
. Here's an example that would have dangling reference issues:
struct Foo {
Foo() = default;
Foo(std::function fn) : fn(fn) { }
std::function fn;
}
Foo outer;
{
Foo inner([&inner](int i){f(i, inner);});
outer = inner;
}
outer.fn(42); // still has reference to inner, which has now been destroyed