Lambda Expression vs Functor in C++

后端 未结 8 795
予麋鹿
予麋鹿 2020-12-05 00:01

I wonder where should we use lambda expression over functor in C++. To me, these two techniques are basically the same, even functor is more elegant and cleaner tha

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 00:14

    Small functions that are not repeated.

    The main complain about functors is that they are not in the same place that they were used. So you had to find and read the functor out of context to the place it was being used in (even if it is only being used in one place).

    The other problem was that functor required some wiring to get parameters into the functor object. Not complex but all basic boilerplate code. And boiler plate is susceptible to cut and paste problems.

    Lambda try and fix both these. But I would use functors if the function is repeated in multiple places or is larger than (can't think up an appropriate term as it will be context sensitive) small.

提交回复
热议问题