Why would I std::move an std::shared_ptr?

前端 未结 6 2009
一整个雨季
一整个雨季 2021-01-29 19:14

I have been looking through the Clang source code and I found this snippet:

void CompilerInstance::setInvocation(
    std::shared_ptr          


        
6条回答
  •  温柔的废话
    2021-01-29 19:57

    I think that the one thing the other answers did not emphasize enough is the point of speed.

    std::shared_ptr reference count is atomic. increasing or decreasing the reference count requires atomic increment or decrement. This is hundred times slower than non-atomic increment/decrement, not to mention that if we increment and decrement the same counter we wind up with the exact number, wasting a ton of time and resources in the process.

    By moving the shared_ptr instead of copying it, we "steal" the atomic reference count and we nullify the other shared_ptr. "stealing" the reference count is not atomic, and it is hundred times faster than copying the shared_ptr (and causing atomic reference increment or decrement).

    Do note that this technique is used purely for optimization. copying it (as you suggested) is just as fine functionality-wise.

提交回复
热议问题