#include <cstdio>
template <typename T>
struct unnamed_lambda
{
unnamed_lambda(T term) : captured_term(term) {}
template <typename A>
unnamed_lambda operator()(A func);
T captured_term;
};
struct terminal_lambda
{
template <typename A>
unnamed_lambda<A> operator()(A term)
{
return unnamed_lambda<A>{term};
}
};
terminal_lambda terminal;
template <typename T>
template <typename A>
unnamed_lambda<T> unnamed_lambda<T>::operator()(A func)
{
return terminal(func(captured_term));
}
struct Hello
{
FILE* operator()(FILE* s)
{
fprintf(s, "Hello\n");
return s;
}
};
struct World
{
FILE* operator()(FILE* s)
{
fprintf(s, "World\n");
return s;
}
};
int main()
{
Hello hello;
World world;
unnamed_lambda<FILE*> l1 = terminal(stdout);
unnamed_lambda<FILE*> l2 = l1(hello);
unnamed_lambda<FILE*> l3 = l2(world);
// same as:
terminal(stdout)(hello)(world);
}
I2luY2x1ZGUgPGNzdGRpbz4KCnRlbXBsYXRlIDx0eXBlbmFtZSBUPgpzdHJ1Y3QgdW5uYW1lZF9sYW1iZGEKewogICAgdW5uYW1lZF9sYW1iZGEoVCB0ZXJtKSA6IGNhcHR1cmVkX3Rlcm0odGVybSkge30KCiAgICB0ZW1wbGF0ZSA8dHlwZW5hbWUgQT4KICAgIHVubmFtZWRfbGFtYmRhIG9wZXJhdG9yKCkoQSBmdW5jKTsKICAgIAogICAgVCBjYXB0dXJlZF90ZXJtOwp9OwoKc3RydWN0IHRlcm1pbmFsX2xhbWJkYQp7CiAgICB0ZW1wbGF0ZSA8dHlwZW5hbWUgQT4KICAgIHVubmFtZWRfbGFtYmRhPEE+IG9wZXJhdG9yKCkoQSB0ZXJtKQogICAgewogICAgICAgIHJldHVybiB1bm5hbWVkX2xhbWJkYTxBPnt0ZXJtfTsKICAgIH0KfTsKCnRlcm1pbmFsX2xhbWJkYSB0ZXJtaW5hbDsKCnRlbXBsYXRlIDx0eXBlbmFtZSBUPgp0ZW1wbGF0ZSA8dHlwZW5hbWUgQT4KdW5uYW1lZF9sYW1iZGE8VD4gdW5uYW1lZF9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