Menu

False positive: uninitMemberVar

CHR
2021-01-11
2021-01-18
  • CHR

    CHR - 2021-01-11
    struct Foo {
        //Foo() = default;
        int i{};
        static const float cf;
    };
    
    const float Foo::cf = 0.1f;
    
    struct Bar {
        Foo f;
        int j{};
    };
    
    int main() {
        Bar b;
        return b.f.i + b.j;
    }
    

    Output:

    "C:\Program Files\Cppcheck\cppcheck.exe" --inconclusive --enable=all foo.cpp
    Checking foo.cpp ...
    foo.cpp:15:13: error: Uninitialized variable: i [uninitvar]
     return b.f.i + b.j;
                ^
    

    cppcheck gets confused by the static const float member. The warning disappears when cf is removed or a default constructor is added to Foo-

     
  • CHR

    CHR - 2021-01-18

    Can anybody reproduce this?

     
  • Daniel Marjamäki

    Thanks! I can reproduce. I created ticket https://trac.cppcheck.net/ticket/10119

     

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.