Menu

Inconsistent operatorEqVarError warning

2017-12-12
2021-01-27
  • Daniel R Slater

    Daniel R Slater - 2017-12-12

    I have a class a header that looks something like this:

    class MyClass {
    public:
        MyClass()=default;
        MyClass(const MyClass &)=default;
        MyClass(MyClass &&)=default;
        ~MyClass()=default;
        MyClass &operator=(MyClass o) {
            swap(o);
            return *this;
        }
        void swap(MyClass &o)noexcept;
    private:
        std::unordered_map<int,std::string> theMap_{};
        // many other std::string member variables 
    }
    

    When I run cppcheck 1.81 on sources that include this header, it gives an operatorEqVarError error complaining that

    "Member variable 'MyClass::theMap_' is not assigned a value in 'MyClass::operator='.

    This is not surprising to me, as cppcheck doesn't see the definition of MyClass::swap(). What is surprising to me is that I actually have several classes that are defined like this, yet cppcheck only complains about this one.

    Should cppcheck complain about this? If so, why doesn't it complain about this same construct in my other class headers? If not, why does it complaine here? Is there some way, short of making swap() inline, for cppcehck to know that swap() is in fact exchaning all the member variables?

     
  • CHR

    CHR - 2021-01-27

    I get no repro with 2.3.

     

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.