Menu

Question about warning not generated - uninitialized variable in template class

2017-08-09
2017-08-09
  • Andrzej Ciereszko

    Hi,

    I am evaluating cppcheck to see if will find uninitialized variables members/variables... of a templated class type. But it doesn't seem to.

    Invoked like so:
    /usr/local/bin/cppcheck --enable=all --xml-version=2

    Here is my test code:

    enum class ViewMode
    {
     One   = 1,
     Two     = 2,
     Three  = 3,
     Four     = 4
    };
    
    class TestClass {
     public:
       TestClass() {}
    };
    
    template<typename T, bool C = std::is_copy_constructible<T>::value>
    class TemplateTest
    {
     public:
    
      TemplateTest() {}
    
      TemplateTest(const T& value)
       : value_(value)
      {}
    
      TemplateTest(const TemplateTest&) = delete;
    
      TemplateTest(TemplateTest<T, C>&& rhs)
          : value_(std::move(rhs.value_))
      {}
    
      TemplateTest(T&& value)
          : value_(std::move(value))
      {}
     private:
      T value_;
    
    };
    
    class StaticAnalysisTest {
    
     public:
      StaticAnalysisTest() {}
    
      void DoSomething() {
    
      }
    
     private:
      ViewMode viewMode_;     //this uninitialized warning is found
      TemplateTest<ViewMode> viewMode2_; //this one is not
    };
    
     
  • Andrzej Ciereszko

    I distilled the problem further:

    class Foo
    {
     private:
      int m_nValue;
     public:
      Foo() {};
      Foo(int value) : m_nValue(value) {}
      int GetValue() { return m_nValue; }
    };
    
    class Bar
    {
     public:
      Bar(){}
    
      void DoSomething() {
        Foo foo;
      }
    };
    

    This does not generate an unitialized variable warning, but when I comment out:

    //Foo(int value) : m_nValue(value) {}

    it does

     
    • orbitcowboy

      orbitcowboy - 2017-08-11

      Thank you for evaluating Cppcheck. A warning is given for the second example, in case you add the --inconclusive flag, for example:

      $ cppcheck --enable=all --inconclusive uninitmembervar.cpp 
      [uninitmembervar.cpp:6]: (warning, inconclusive) Member variable 'Foo::m_nValue' is not initialized in the constructor.
      
       
      • Andrzej Ciereszko

        That works, thanks!!

         
  • Andrzej Ciereszko

    That works, thanks!!

     
  • Andrzej Ciereszko

    Ok a quick follow up, while that works, it still does not in the first example I gave. Here is distilled example of the template case:

    template<typename T, bool C = std::is_copy_constructible<T>::value>
    class Observable
    {
    
        public:
          Observable() {}
    
        private:
          T value_;
    
    };
    
    class Bah
    {
     public:
      Bah() {};
    
     private:
    
      Observable<int> tValue_;
    
    };
    

    This does not give a warning about tValue not being initialized. But if I remove the second parameter of the template (bool C), it does warn about tValue not being initialized.

    Any idea why?

     
    • orbitcowboy

      orbitcowboy - 2017-08-13

      Hi,
      it seems that you have found a case, where Cppcheck is currently blind. I have created a ticket about this test case on our bugtracker #8162.

      Many thanks for evaluating Cppcheck!

       

      Last edit: orbitcowboy 2017-08-13

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.