Capture occurs before construction std::function.
So, you commit an uninitialized (not even constructed by default) copy std::function<void()> func. The capture itself std::functionis UB (copying the variable before it is built!), And when called, it will even be "larger than UB" (causing a copy of the non-built std::function!).
func, , , .
, func. , func . , , .
" " , - y-combinator.
++ 14 y-combinator:
template<class F>
auto y_combinate( F&& f ) {
return [f = std::forward<F>(f)](auto&&...args) {
return f(f, decltype(args)(args)...);
};
}
, :
std::function<void ()> func = y_combinate( [](auto&& self) {
self(self);
}
);
.
y-combinator , this . .
y-combinator 90%, r/l . .
y-combinate:
template<class F>
struct y_combinate_t {
F f;
template<class...Args>
decltype(auto) operator()(Args&&...args)const {
return f(*this, std::forward<Args>(args)...);
}
};
template<class F>
y_combinate_t<std::decay_t<F>> y_combinate( F&& f ) {
return {std::forward<F>(f)};
}
:
std::function<void ()> func = y_combinate( [](auto&& self) {
self();
}
);
self, , self .