Menu

More than 1 template initializer yields warning

2018-04-18
2018-04-20
  • Sheldon Teerlink

    // Test Case:

    // When: cppcheck --enable=warning warning.cpp

    // Expect: Zero warnings

    // Actual: (warning) Member variable 'Test::_warning_map' is not initialized

    // in the constructor.

    // Synopsis: Warning is generated for templated types with more than one

    // identifier. No warning is generated for '_warning_vector' which

    // only has one identifier.

    include

    include <string>

    include <vector>

    class Test {

    public:

    Test() {}

    protected:

    std::string* _warning = nullptr;

    std::vector<std::string>* _warning_vector = nullptr;

    std::map<int, int="">* _warning_map = nullptr;

    };

    int main(int argc, char* argv[]) {

    Test();

    return 0;

    }

     
  • versat

    versat - 2018-04-19

    Thank you for the report.
    Please use the code formatting syntax like this:

    ~~~
    code goes here
    ~~~

    Otherwise the code will be shown wrong.
    I've extracted this from the attachment:

    // Test Case:
    //    When:     cppcheck --enable=warning warning.cpp
    //    Expect:   Zero warnings
    //    Actual:   (warning) Member variable 'Test::_warning_map' is not initialized
    //              in the constructor.
    //    Synopsis: Warning is generated for templated types with more than one
    //              identifier. No warning is generated for '_warning_vector' which
    //              only has one identifier.
    
    #include <map>
    #include <string>
    #include <vector>
    
    class Test {
    public:
      Test() {}
    
    protected:
      std::string* _warning = nullptr;
      std::vector<std::string>* _warning_vector = nullptr;
      std::map<int, int>* _warning_map = nullptr;
    
    };
    
    int main(int argc, char* argv[]) {
      Test();
    
      return 0;
    }
    

    I can reproduce the warning with the latest development version.
    I am not absoulutely sure if this ticket already reports the same or just a very similar/related problem:
    https://trac.cppcheck.net/ticket/8146
    Another related ticket that is missing the case reported here is https://trac.cppcheck.net/ticket/5499.
    It seems like the direct member initialization inside a class definition that is new in C++11 is not yet handled correctly.

     
  • versat

    versat - 2018-04-19

    std::map somehow seems to be special here, so i created ticket 8498

     
  • Sheldon Teerlink

    In my tests I discovered std::pair<int, int=""> also yields a warning. As does MyClass<int, int="">. However, MyClass<int> does not.

    Thank you for looking into this.

     
    • versat

      versat - 2018-04-20

      amai detected something similar and wrote about it in a ticket comment.
      It seems that there is a problem as soon as more than one template parameter is used.

       

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.