5 ms·
I like to pass closures as return values or as function parameters. In my experience this is folly in c++.
by usgroup 8y ago
I like to pass closures as return values or as function parameters. In my experience this is folly in c++.
- grandmczeb 8y agoHere's an example of doing both: $ cat test.cpp #include <iostream> #include <string> template <typename F> auto pass_func(F func) { return func(); } auto return_func() { std::string test = "Hello World!"; return [=](){ return test; }; } int main() { auto func = return_func(); std::cout << pass_func(func) << std::endl; } $ clang test.cpp -lstdc++ -std=c++14 -o test $ ./test "Hello World!" What's hard about it?
- black-tea 8y agoYou didn't pass a function to std::cout, you called it and passed its return value.
- grandmczeb 8y agoThe way I interpret the GP, they're talking about passing closures as a function argument. In the example I pass func (a closure) to pass_func. Not sure what std::cout has to do with it?
- black-tea 8y agoSorry, I misread the code.