Menu

False positive uninitMemberVar with std template specializations

2024-12-16
2024-12-17
  • Frank Winklmeier

    Having a member variable named hash seems to clash with template specializations of std::hash. The following snippet

    struct Foo {};
    
    namespace std {
      template<>
      struct hash<Foo> {};
    }
    
    struct Bar
    {
      Bar(int the_hash) : hash(the_hash) {}
      int hash;
    };
    

    results in

    test.cxx:10:3: warning: Member variable 'Bar::hash' is not initialized in the constructor. [uninitMemberVar]
      Bar(int the_hash) : hash(the_hash) {}
      ^
    

    If you rename the member to e.g. _hash the warning disappears. The same happens for other template specializatons, e.g. std::size and a member variable named size.

     
  • Frank Winklmeier

    Here is another reproducer that may or may not be the same problem:

    template <class T>
    struct Foo
    {
      Foo() = default;
      void store ();
    };
    
    
    template <class T>
    void Foo<T>::store()
    {}
    
    Foo<int> foo;
    void* store = nullptr;
    

    results in cppcheck complaining about a non-existent store member:

    test.cxx:4:3: warning: Member variable 'Foo < int >::store' is not initialized in the constructor. [uninitMemberVar]
      Foo() = default;
      ^
    
     

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.