Home > Mobile >  When to not use lambdas over normal functions?
When to not use lambdas over normal functions?

Time:10-25

I am aware how a lambda works, I use lambdas pretty much everywhere in my code, is there any scenario where I should prefer using normal functions instead of lambdas

CodePudding user response:

When to not use lambdas over normal functions?

Using a normal function is often preferable when the function can be re-used in multiple contexts.

CodePudding user response:

When not to use lambdas:

  1. When the function is declared in a header and implemented in a .cpp files. Can't do this with lambda.

  2. When the function is a template, and you want to be able to manually specify template arguments. Doing this with lambas requires an ugly syntax: foo.operator()<...>(...).

When to use lambdas:

  1. When the function is overloaded/templated, and you want to conveniently pass it as an argument to a different function.

  2. You want to avoid ADL.


Other than that, preferring regular functions over lambdas is just a convention.

If you want to go against this convention, you should be prepared to explain your reasoning.

  • Related