Search code examples
move-semanticscppcheck

Is cppcheck aware of move semantics?


I'm writing a class that more or less looks like this

struct Foo {
    std::shared_ptr<Bar> bar_ptr;

    Foo(std::shared_ptr<Bar> b)
      : bar_ptr(std::move(b)) {}
};

and cppcheck warns

(performance) Function parameter 'b' should be passed by reference.

I was under the impression that in this case b should not be any sort of reference. This would allow instances of Foo to optionally take complete control of the resource (the original shared pointer) vis-a-vis

auto bar1 = std::make_shared<Bar>(...);

Foo my_foo(bar1); // bar1 remains untouched
Foo your_foo(std::move(bar1)); // takes complete control of the bar1 
                               // resource; bar1 now invalid

Am I not using something correctly, or is this not something cppcheck is aware of yet?


Solution

  • I am a Cppcheck developer. It seems to me this is a false positive, Cppcheck should not warn. Probably we should not warn for std::shared_pointer parameters in any functions. I created this ticket: http://trac.cppcheck.net/ticket/8400