How to avoid C++ anonymous objects

后端 未结 7 1707
甜味超标
甜味超标 2020-12-06 10:21

I have a ScopedLock class which can help to release lock automatically when running out of scope. However, the problem is: Sometimes team members write invalid

7条回答
  •  失恋的感觉
    2020-12-06 11:09

    replace it with macro

    #define CON2(x,y) x##y
    #define CON(x,y) CON2(x,y)
    #define LOCK(x)  ScopedLock CON(unique_,__COUNTER__)(mutex)
    

    usage

    {
      LOCK(mutex);
      //do stuff
    }
    

    This macro will generate unique names for locks, allowing lockeng of other mutexes in inner scopes

提交回复
热议问题