I have seen an interesting trick in one codebase, but it only works if your scoped_lock type is not a template (std::scoped_lock is).
#define scoped_lock(x) static_assert(false, "you forgot the variable name")
If you use the class correctly, you have
scoped_lock lock(mutex);
and since the scoped_lock identifier isn't followed by an open paren, the macro won't trigger and the code will remain as it is. If you write
scoped_lock(mutex);
the macro will trigger and the code will be substituted with
static_assert(false, "you forgot the variable name");
This will generate an informative message.
If you use a qualified name
threads::scoped_lock(mutext);
then the result will still not compile, but the message won't be as nice.
Of course, if your lock is a template, the bad code is
scoped_lock<mutex_type>(mutex);
which won't trigger the macro.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…