Skip to content

Instantly share code, notes, and snippets.

@yume-chan
Last active March 8, 2018 05:22
Show Gist options
  • Save yume-chan/fd6a6c32afec7c99f9d15206befd81e9 to your computer and use it in GitHub Desktop.
Save yume-chan/fd6a6c32afec7c99f9d15206befd81e9 to your computer and use it in GitHub Desktop.
#include <functional>
#include <iostream>
template <class F>
struct wrapper {
wrapper(F&& f)
: f(std::forward<F>(f)) { }
std::decay_t<F> f;
};
template <class F>
decltype(auto) make_wrapper(F&& f) {
return wrapper<F>(std::forward<F>(f));
}
decltype(auto) test_lambda() {
auto s = std::string("hello");
auto l = [s]()->void {
std::cout << s.c_str() << std::endl;
};
auto c = l;
return c;
}
decltype(auto) test_wrapper() {
auto s = std::string("hello");
auto c = [s]()->void {
std::cout << s.c_str() << std::endl;
};
return make_wrapper(c);
}
int main()
{
auto l = test_lambda();
l();
auto w = test_wrapper();
w.f();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment