Menu

Its hard to find problem in template

2018-05-11
2018-05-14
  • Paolo Biagini

    Paolo Biagini - 2018-05-11
    template<class T>
    class TestClass
    {
    public:
      TestClass<T>()
      {
         memset( &m_hi , 1 , sizeof(m_hi));
      }
    
      T m_hi;
    };
    
    struct v {  virtual ~v() {} };
    struct f {  float f=0.0f; };
    struct z {  std::auto_ptr<v> av; };
    
    int main()
    {
       TestClass<std::string> objTest1;
       TestClass<v>  objTest2;
       TestClass<std::auto_ptr<v>> objTest3;
       TestClass<z> objTest4;
       TestClass<f> objTest5;
       TestClass<int> objTestOK;
    }
    

    [test.cpp:7]: (error) Using 'memset' on struct that contains a virtual method is unsafe, because constructor, destructor and copy operator calls are omitted. These are necessary for this non-POD type to ensure that a valid object is created.

    I have no error for each template, and no information for find the wrong template used :-(

     
  • Robert Reif

    Robert Reif - 2018-05-11

    Cppcheck is unable to instaniate the third variable:

    [forum.cpp:21]: (debug) Failed to instantiate template "TestClass". The checking continues anyway.
    

    and gives up so all templates after it are also not instantiated.

     
  • Robert Reif

    Robert Reif - 2018-05-14
     

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.