Suppose that I have a requirement that if an operation is performed within a scope, then it must be undone in the same scope. An example of that is entering and leaving a critical section.
In order to enforce users to use paired do - undo operations, a pair of macros is defined that use open and close braces:
#define BEGIN \
{ \
do_something();
#define END \
undo_something(); \
}
Of course, there are ways that a "malicious" coder can trick those macros (e.g. by adding a an opening or closing brace), but generally this helps remembering that a BEGIN
must be followed by an END
. Moreover, if, for example, an existing BEGIN
is commented out, the compiler will complain, indicating that the END
must be removed also.
I have seen this is several internal projects. As I said, it does not offer 100% protection but could this practice prove harmful in any way? Is this a well-known practice?
begin()
andend()
fordo_something
andundo_something
? OrBegin()
andEnd()
if you want them to stand out more and your capitalization finger needs exercise.