Twój kod nie działa. Ale to robi:
template<class F>
struct ycombinator {
F f;
template<class...Args>
auto operator()(Args&&...args){
return f(f, std::forward<Args>(args)...);
}
};
template<class F>
ycombinator(F) -> ycombinator<F>;
Kod testowy:
ycombinator bob = {[x=0](auto&& self)mutable{
std::cout << ++x << "\n";
ycombinator ret = {self};
return ret;
}};
bob()()(); // prints 1 2 3
Twój kod jest zarówno UB, jak i źle sformułowany, nie jest wymagana diagnostyka. Co jest zabawne; ale oba można naprawić niezależnie.
Po pierwsze, UB:
auto it = [&](auto self) { // outer
return [&](auto b) { // inner
std::cout << (a + b) << std::endl;
return self(self);
};
};
it(it)(4)(5)(6);
to jest UB, ponieważ zewnętrzny przyjmuje selfwartość, a następnie wewnętrzny przechwytuje selfprzez odniesienie, a następnie zwraca go po outerzakończeniu działania. Więc segfaulting jest zdecydowanie w porządku.
Poprawka:
[&](auto self) {
return [self,&a](auto b) {
std::cout << (a + b) << std::endl;
return self(self);
};
};
Kod pozostaje źle sformułowany. Aby to zobaczyć, możemy rozszerzyć lambdy:
struct __outer_lambda__ {
template<class T>
auto operator()(T self) const {
struct __inner_lambda__ {
template<class B>
auto operator()(B b) const {
std::cout << (a + b) << std::endl;
return self(self);
}
int& a;
T self;
};
return __inner_lambda__{a, self};
}
int& a;
};
__outer_lambda__ it{a};
it(it);
to tworzy wystąpienie __outer_lambda__::operator()<__outer_lambda__>:
template<>
auto __outer_lambda__::operator()(__outer_lambda__ self) const {
struct __inner_lambda__ {
template<class B>
auto operator()(B b) const {
std::cout << (a + b) << std::endl;
return self(self);
}
int& a;
__outer_lambda__ self;
};
return __inner_lambda__{a, self};
}
int& a;
};
Następnie musimy określić typ zwracania __outer_lambda__::operator().
Przechodzimy przez to linijka po linijce. Najpierw tworzymy __inner_lambda__typ:
struct __inner_lambda__ {
template<class B>
auto operator()(B b) const {
std::cout << (a + b) << std::endl;
return self(self);
}
int& a;
__outer_lambda__ self;
};
A teraz spójrz tam - jego typ zwracany to self(self)lub __outer_lambda__(__outer_lambda__ const&). Ale jesteśmy w trakcie próby ustalenia zwracanego typu __outer_lambda__::operator()(__outer_lambda__).
Nie możesz tego zrobić.
Chociaż w rzeczywistości zwracany typ __outer_lambda__::operator()(__outer_lambda__)nie jest w rzeczywistości zależny od zwracanego typu __inner_lambda__::operator()(int), C ++ nie przejmuje się dedukowaniem zwracanych typów; po prostu sprawdza kod linia po linii.
I self(self)jest używany, zanim go wydedukowaliśmy. Źle ukształtowany program.
Możemy to załatać, ukrywając na self(self)później:
template<class A, class B>
struct second_type_helper { using result=B; };
template<class A, class B>
using second_type = typename second_type_helper<A,B>::result;
int main(int argc, char* argv[]) {
int a = 5;
auto it = [&](auto self) {
return [self,&a](auto b) {
std::cout << (a + b) << std::endl;
return self(second_type<decltype(b), decltype(self)&>(self) );
};
};
it(it)(4)(6)(42)(77)(999);
}
a teraz kod jest poprawny i kompiluje się. Ale myślę, że to trochę hack; po prostu użyj ycombinator.