Menu

ctuOneDefinitionRuleViolation false positive with nested classes?

netjeff
2022-02-15
2022-02-15
  • netjeff

    netjeff - 2022-02-15

    File nested.cc contains

    class OuterAbc
    {
        class Inner;
    };
    
    class OuterAbc::Inner { };
    
    class OuterXyz
    {
        class Inner;
    };
    
    class OuterXyz::Inner { };
    

    cppcheck produces the following output for this nested.cc

    Checking nested.cc ...
    nested.cc:6:1: error: The one definition rule is violated, different classes/structs have the same name 'Inner' [ctuOneDefinitionRuleViolation]
    class OuterAbc::Inner { };
    ^
    nested.cc:13:1: note: The one definition rule is violated, different classes/structs have the same name 'Inner'
    class OuterXyz::Inner { };
    ^
    nested.cc:6:1: note: The one definition rule is violated, different classes/structs have the same name 'Inner'
    class OuterAbc::Inner { };
    ^
    

    According to https://en.cppreference.com/w/cpp/language/nested_types

    The name of the nested class exists in the scope of the enclosing class

    It seems like this nested.cc is defining two different classes 'OuterAbc::Inner' and 'OuterXyz::Inner'. But cppcheck is complaining about multiple definitions of plain 'Inner' ?

     
  • CHR

    CHR - 2022-02-15

    Which version do you use? I cannot reproduce this with head.

     
  • netjeff

    netjeff - 2022-02-15

    When I use latest released 2.7 no error. Turns out I was using 2.4.1. I should have checked version to be sure latest, doh.

    Thanks.

     

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.