#include <iostream>
#include <functional>
struct PassMe
{
PassMe()
{
std::cout << "default" << std::endl;
}
PassMe(PassMe const &)
{
std::cout << "copy" << std::endl;
}
PassMe(PassMe &&)
{
std::cout << "move" << std::endl;
}
PassMe &operator=(PassMe const &)
{
std::cout << "copy assign" << std::endl;
return *this;
}
PassMe &operator=(PassMe &&)
{
std::cout << "move assign" << std::endl;
return *this;
}
~PassMe()
{
std::cout << "destruct" << std::endl;
}
void go() const
{
std::cout << "go" << std::endl;
}
};
template<typename T>
auto f(T const &t)
-> std::function<void()>
{
std::cout << "before" << std::endl;
std::function<void()> l {[t]{t.go();}};
std::cout << "after" << std::endl;
return l;
}
int main()
{
PassMe pm;
std::cout << "pre-call" << std::endl;
auto l = f(pm);
std::cout << "post-call" << std::endl;
l();
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KCnN0cnVjdCBQYXNzTWUKewoJUGFzc01lKCkKCXsKCQlzdGQ6OmNvdXQgPDwgImRlZmF1bHQiIDw8IHN0ZDo6ZW5kbDsKCX0KCVBhc3NNZShQYXNzTWUgY29uc3QgJikKCXsKCQlzdGQ6OmNvdXQgPDwgImNvcHkiIDw8IHN0ZDo6ZW5kbDsKCX0KCVBhc3NNZShQYXNzTWUgJiYpCgl7CgkJc3RkOjpjb3V0IDw8ICJtb3ZlIiA8PCBzdGQ6OmVuZGw7Cgl9CglQYXNzTWUgJm9wZXJhdG9yPShQYXNzTWUgY29uc3QgJikKCXsKCQlzdGQ6OmNvdXQgPDwgImNvcHkgYXNzaWduIiA8PCBzdGQ6OmVuZGw7CgkJcmV0dXJuICp0aGlzOwoJfQoJUGFzc01lICZvcGVyYXRvcj0oUGFzc01lICYmKQoJewoJCXN0ZDo6Y291dCA8PCAibW92ZSBhc3NpZ24iIDw8IHN0ZDo6ZW5kbDsKCQlyZXR1cm4gKnRoaXM7Cgl9Cgl+UGFzc01lKCkKCXsKCQlzdGQ6OmNvdXQgPDwgImRlc3R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