Menu

False positive: incorrect const suggestion when overloading operator() [constParameter]

2020-06-29
2021-01-26
  • Stefan Löffler

    Stefan Löffler - 2020-06-29
    struct A {
        int i{0};
        const int & operator()() const { return i; }
        int & operator()() { return i; }
    };
    
    int getVal(const A & a) { return a(); }
    void setVal(A & a) { a() = 1; }
    

    produces

    function-call-overload.cpp:8:17: style: Parameter 'a' can be declared with const [constParameter]
    void setVal(A & a) { a() = 1; }
                    ^
    

    which is incorrect because the non-const overload has to be used. Interestingly, this error goes away when using operator[] instead of operator().
    This is closely related to #9266 and may also be related to issues #9759, #9760,

     
  • CHR

    CHR - 2021-01-26

    I can't reproduce this with 2.3.

     

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.