Menu

Potential false positive: Partial explicit template specialization results in ODR violation on cppcheck

2022-12-14
2022-12-15
  • Muhammad Javed

    Muhammad Javed - 2022-12-14

    I have two different partial explicit class template specializations that results in an ODR violation by cppcheck which I believe to be a false positive. I could be wrong. Regardless, I am having this error present on a recent build from master branch. Here is a sample case that replicates this error:

    PrimaryTemplate.hpp

    template<typename T1, typename T2>
    struct Test
    {};
    

    A.cpp

    #include "PrimaryTemplate.hpp"
    
    template<typename T1>
    struct Test<T1, int> // cppcheck says ODR violation here
    {};
    

    B.cpp

    #include "PrimaryTemplate.hpp"
    
    template<typename T1>
    struct Test<T1, double> // cppcheck says ODR violation here as well
    {};
    

    This does not look like an ODR violation to me. However, if it is then I am very sorry for wasting anybody's time. Thanks!

     
  • Daniel Marjamäki

    the code looks strange. is Test supposed to be a common base class?

     
  • Muhammad Javed

    Muhammad Javed - 2022-12-14

    Sorry for the strange nature of the code, I tried to replicate the code that was producing the ODR violation in my project into a simple case and whittled out the unnecessary details.

    In my project, the struct which I based Test off of is not a common base class. Rather, there is a macro defined in my project which takes in a single type and creates specializations of that struct. Each use of the macro creates a ctuOneDefinitionRuleViolation on cppcheck.

     

    Last edit: Muhammad Javed 2022-12-14
  • CHR

    CHR - 2022-12-15

    How do you call cppcheck? I'm failing to reproduce this so far.

     
  • Muhammad Javed

    Muhammad Javed - 2022-12-15

    The only argument I pass to cppcheck is the directory containing those 3 files I posted above. I even double checked to make sure I was running the cppcheck that I built yesterday (2.10 dev).

    After double checking to make sure that the code is the same as in the one I provided, I ran cppcheck again and I have attached the results:

     
  • CHR

    CHR - 2022-12-15

    I had run cppcheck on a single file, not on the directory before.
    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11435

     
  • Muhammad Javed

    Muhammad Javed - 2022-12-15

    Thank you so much!

     

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.