jeudi 26 mai 2016

Nesting Lambda functions - Performance implications

I have a top level lambda function and then there are couple of nested lambda functions inside this lambda.

Is it a good idea to nest lambdas inside other lambdas? Are there any performance implications?

For e.g.

auto Multiplier1 = []() -> int
{
    auto Multiplier2 = [](int i) -> int
    {
        auto Multiplier3 = [](int i) -> int
        {
            return i * 2;
        };
        std::cout << "Calling another lambda 2\n";
        return Multiplier3(i * 100);
    };

    int i = 10;
    std::cout << "Calling another lambda 1\n";
    int ret = Multiplier2(i);
    return ret;
};

int ret = Multiplier1();
std::cout << ret << "\n";

In the example above, I can re-factor Multiplier2 and Multiplier3 into functions. Is that a better approach compare to this?

I am modifying a code which is already in production and hence, I was in a dilemma whether to re-factor it into separate functions or manage using lambda functions.

Aucun commentaire:

Enregistrer un commentaire