Menu

function value parameters that are moved

Fraser
2023-09-16
2023-09-16
  • Fraser

    Fraser - 2023-09-16

    CPPCheck 2.11 reports that a value parameter should be const & qualified when the function uses std::move on the argument. This is typically for moving a new object into a container. The object is created for a function parameter and then the function moves it into the container. I don't think that CPPCheck is tackling move semantics well curently.

     
  • CHR

    CHR - 2023-09-16

    There is no warning with head for

    struct S {
        std::string m;
        void f(std::string s);
    };
    void S::f(std::string s) {
        m = std::move(s);
    }
    
     
  • Fraser

    Fraser - 2023-09-16

    Whats head? In your example m is constructed before the move. Copy assignment would be better.

     
  • CHR

    CHR - 2023-09-16

    Please provide a sample that reproduces the issue.

     
  • Fraser

    Fraser - 2023-09-16

    class NameContainer {
    public:
    explicit NameContainer(std::string /in file name/) noexcept;

    private:
    std::string inName_;
    };

    NameContainer::NameContainer(std::string inName) noexcept : inName_(std::move(inName)) {
    }

     
  • CHR

    CHR - 2023-09-16
     
  • Fraser

    Fraser - 2023-09-16

    I wasn't aware that noexcept was affecting this. It is interesting and possibly useful that the constructor can be declared noexcept since the argument object would throw during its construction before the construction of the NameContainer which now won't throw.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.