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

前端 未结 2 1850
耶瑟儿~
耶瑟儿~ 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:11

    If you're going to define a macro, I'd probably rather define this one:

    #define GET_SCOPED_LOCK(name, mtx) ScopedLock name(mtx)
    

    and stop creating objects other than via the macro.

    Then rename ScopedLock to ThisClassNameShouldNotAppearInUserCode if that helps.

提交回复
热议问题