Menu

False Positive when using (*this)(...) instead of this->operator()(...)

2020-09-07
2020-09-07
  • Fredrik Bondesson

    Following scenario does not seem to be correct (using cppcheck 2.1 gui):
    Using " return (*this)(i, i);" causes

    (error) Reference to temporary returned. [returnTempReference]

    while using
    "return this->operator()(i, i);"
    does not.

    As far as I know, the latter case is correct and no error should be issued.

    #include <iostream>
    
    class Base
    {
    public:
        Base() : elements{1,2,3,4,5} {
        }
    
        double& operator()(unsigned int i, unsigned int j)
        {
            return elements[i+j];
        }
    
        double& operator()(unsigned int i)
        {    
            //return this->operator()(i, i);
            return (*this)(i, i);
        }
    
        double elements[5];
    };
    
    int main()
    {
        Base b;
        std::cout<<"b(0): "<<b(0)<<"\n";
    }
    
     

    Last edit: Fredrik Bondesson 2020-09-07
  • Daniel Marjamäki

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

     

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.