When are stateless class functors useful in place of a c style function?

前端 未结 5 467
猫巷女王i
猫巷女王i 2021-01-05 08:38

I\'ve found some good examples of functors on SO like this one, and all the convincing examples seem to use state in the class that defines operator().

5条回答
  •  被撕碎了的回忆
    2021-01-05 09:23

    Others have made good points about the ability for the compiler to inline the functor. One other possibile advantage of functor objects vs. function pointers is flexibility. The functor might be a template, maybe a derived class, perhaps it has run time configuration (even if stateless at the time operator() is called etc.

提交回复
热议问题