How to avoid C++ anonymous objects

2020-02-01 03:29发布

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 lock-code such as

{
    ScopedLock(mutex);   // anonymous
    xxx;
}

The above code is wrong because the ScopedLock object is constructed and destructed immediately, so it fails to lock the expected area (xxx). I want the compiler to give an error when trying to compile such code. Can this be done?

I have searched g++ warning options, but fail to find the right one.

标签: c++ g++
7条回答
神经病院院长
2楼-- · 2020-02-01 04:21

AFAIK there's no such a flag in gcc. A static analyzer may better suit your needs.

查看更多
登录 后发表回答