Live Hacking: Handwritten Functors (Too Much Boilerplate)

Original Lambda Demo Proggy

#include <functional>
#include <string>
#include <iostream>


static std::function<void()> create_print_function(const std::string& message)
{
    auto print_message = [message](){    // <--- message is 'captured into the closure of function object "print_message"'
        std::cout << message << std::endl;
    };
    return print_message;
}

int main()
{
    auto p = create_print_function("howdy");
    p();
    return 0;
}

Morph Lambda Into Handwritten Functor Class

#include <functional>
#include <string>
#include <iostream>

class print_message_func
{
public:
    print_message_func(const std::string& message) : _message(message) {}
    void operator()()
    {
        std::cout << _message << std::endl;
    }
private:
    std::string _message;
};


static std::function<void()> create_print_function(const std::string& message)
{
    auto print_message = print_message_func(message);
    return print_message;
}

int main()
{
    auto p = create_print_function("howdy");
    p();
    return 0;
}
$ code/c++11-lambda-capture-handwritten
howdy