Menu

Member not initialized false positive

2017-02-17
2017-02-17
  • Alexey Biryukov

    Alexey Biryukov - 2017-02-17

    I should have posted that to trac but I still don't have confirmation email received.

    struct Parser
    {
        Parser& parse(int&);
    };
    
    struct X
    {
        int x, y;
        explicit X( Parser& p ) 
        {
            p.parse(x).parse(y);
    //      p.parse(y);
        }
    };
    
    void main()
    {
        X x ( Parser() );
        std::cout << x.x << x.y;
    }
    

    cpp-check 1.77 issues a warning for that:

    (warning) Member variable 'X::y' is not initialized in the constructor.

    No warning is issued for X::x at the same time.
    If the commented line is uncommented, no warning is issued.

    As far as I understand this, whenever a member variable is referenced in a call from ctor code, it's considered initialized by that code. (BTW this is true even if parse() takes its argument by value rather than by reference, which is ridiculous.) However the second element in the chained call to parse() appears to be ignored, which causes false positive for y.

     

    Last edit: Alexey Biryukov 2017-02-17
  • Daniel Marjamäki

    hi! sorry for late reply. please try the verification code 'Js6luWrz'.

    BTW this is true even if parse() takes its argument by value rather than by reference, which is ridiculous.

    yes that is not good.

     

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.