Lambda to std::function conversion performance

前端 未结 2 1393
被撕碎了的回忆
被撕碎了的回忆 2021-01-21 06:26

I\'d like to use lambda functions to asynchronously call a method on a reference counted object:

void RunAsync(const std::function& f) { /* ..         


        
相关标签:
2条回答
  • 2021-01-21 06:41

    std::function probably won't be as fast as a custom functor until compilers implement some serious special treatment of the simple cases.

    But the reference-counting problem is symptomatic of copying when move is appropriate. As others have noted in the comments, MSVC doesn't properly implement move. The usage you've described requires only moving, not copying, so the reference count should never be touched.

    If you can, try compiling with GCC and see if the issue goes away.

    0 讨论(0)
  • 2021-01-21 06:47

    Converting to a std::function should only make a move of the lambda. If this isn't what's done, then there's arguably a bug in the implementation or specification of std::function. In addition, in your above code, I can only see two copies of the original c, one to create the lambda and another to create the std::function from it. I don't see where the extra copy is coming from.

    0 讨论(0)
提交回复
热议问题