3

Possible Duplicate:
prohibiting instantiation as a temporary object (C++)

I use a Scopeguard for locking like this (simplified):

{
ScopeGuard sg(mutex);
// ...critical code
}

I accidently typed in some place

{
ScopeGuard(mutex);
// ...critical code
}

which is valid code but does not extend the lifetime of the ScopeGuard object past the statement.

Question: Is there any pattern that will result in a compiler error or warning if I create a temporary ScopeGuard object like in the second example?

Community
  • 1
  • 1
Gabriel Schreiber
  • 2,166
  • 1
  • 20
  • 33

1 Answers1

1

I'd leverage the preprocessor to define a lock pseudo-keyword, as described here: http://www.curly-brace.com/lock.html

Daniel Gehriger
  • 7,339
  • 2
  • 34
  • 55
  • 1
    As it often happens with macros, the cure is worse than disease. Forgetfulness goes away with experience. – Gene Bushuyev Jan 14 '11 at 15:15
  • Granted - I avoid using macros myself. But if you cannot count on the experience of co-workers or library users, then they can help ensuring correct usage of code patterns. – Daniel Gehriger Jan 14 '11 at 16:37