Menu

False positive: functionConst with overloaded operator++

CHR
2021-01-05
2021-01-05
  • CHR

    CHR - 2021-01-05
    struct Foo
    {
        int i{};
        Foo& operator ++() { ++i; return *this; } // prefix
        Foo operator ++(int) { Foo f = *this; ++(*this); return f; } // postfix
    };
    

    Calling v2.3 with "c:\Program Files\Cppcheck\cppcheck.exe" --enable=all --inconclusive foo.cpp results in

    foo.cpp:5:6: style:inconclusive: Technically the member function 'Foo::operator++' can be const. [functionConst]
     Foo operator ++(int) { Foo f = *this; ++(*this); return f; } // postfix
         ^
    

    Apparently, the call to the non-const prefix operator++ is not recognized.

     
  • Daniel Marjamäki

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

     

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.