3

when I am going through the lambda function, I have seen people comparing lambda with functors & I came across a statement
users don't have to clutter their code with small functors in some accessible scope.

My doubt is

  1. what is the problem in having small functors in some accessible scope
  2. isn't it good idea to have a single function (functor actually) & reuse it across multiple files in our project.

Thanks.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
NDestiny
  • 1,133
  • 1
  • 12
  • 28
  • Function and functor is a too different things: functor is a structure/class that behaves like a function. And lambdas are usually one-time functions and used in one or two places only. – m0nhawk May 13 '16 at 13:36
  • What do you mean by "some accessible scope" ? – WhiZTiM May 13 '16 at 13:40
  • @WhiZTiM I took it from here (http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11) , I think he means put it in some header file & use it in different files by including that header filet. – NDestiny May 13 '16 at 13:44
  • @m0nhawk do you mean, we can't compare them in any way right ? & my question is different.. – NDestiny May 13 '16 at 13:45
  • @Durga It's possible to use one on place of the other (with some limitations of passing and storing). But there is a different choice for various situation: one-time usage - lambda; function - function; and smart function (with parametrisation, statefulness) - functor. – m0nhawk May 13 '16 at 13:52

1 Answers1

3
  1. it is unnecessary if you have to use each only once. Lambdas usually makes the code more readable, the function is defined exactly at the place it is needed.
  2. this is not always the case, a function may be called at only one place. Of course if you needed it at different places a functor may be more appropriate.
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69