Concerning RAII: How to prevent errors caused by accidentally creating a temporary?

前端 未结 2 1870
耶瑟儿~
耶瑟儿~ 2021-01-04 17:07

A while age a colleague told me he spent lot of time debugging a race condition. The culprit turned out to be something like this:

void foo()
{
    ScopedLoc         


        
2条回答
  •  天命终不由人
    2021-01-04 18:02

    You should use a static code analyser such as Cppcheck. For the following code:

    class a { };
    
    void f() {
        a();
    }
    

    cppcheck generates the following output:

    $ cppcheck test.cpp
    Checking test.cpp...
    [test.cpp:4]: (error) instance of "a" object destroyed immediately
    

    There are a wide variety of other common coding errors also detected.

    (I'm a fairly new contributor to Cppcheck. I found it a couple of months ago and it's been fantastic working with it.)

提交回复
热议问题