After my initial post about currying in C++, I was wondering whether it is possible to make a universal curry transformation that will be applicable to any functor.

Fortunately, like most weird things, in C++ the answer to this question is yes.

Here, we’ll create a curry object from a lambda function:

auto log = make_curry(
    [] (int type, int subtype, const std::string & message)
    {
        std::cerr
            << type << " / " << subtype
            << "\t" << message << std::endl;
    }
);

And use it in all the different ways:

int main(int argc, const char *argv[])
{
    auto message = log(1);
    auto error = log(0, 0);

    // Only one argument already given
    message(0, "Normal message");
    message(1, "Important message");

    // We have two arguments already given
    error("No error, just kidding");

    // Or, we can call directly
    log(1, 1, "Arguments: 3 - 0")();
    log(1, 1)("Arguments: 2 - 1");
    log(1)(2, "Arguments: 1 - 2");
    log()(2, 2, "Arguments: 0 - 3");

    return 0;
}

It allows only to split the function arguments into two sets. I might make it a bit more powerful at some point. I’m a bit sleepy now.

Anyone interested in the code?