1 import checkedint.sticky : raise; // set IntFlagPolicy.sticky 2 3 string[] log; 4 5 void onlyZero(int x) 6 { 7 mixin(IntFlags.pushPop); 8 9 if (x < 0) 10 raise(IntFlag.negOver); 11 if (x > 0) 12 raise(IntFlag.posOver); 13 14 if (IntFlags.local) 15 log ~= IntFlags.local.clear().toString(); 16 } 17 18 IntFlags.local = IntFlag.imag; 19 onlyZero(-50); 20 onlyZero(22); 21 onlyZero(0); 22 assert(IntFlags.local.clear() == IntFlag.imag); 23 24 assert(log == ["{negative overflow}", "{positive overflow}"]);
A mixin string that may be used to (effectively) push a new IntFlags.local variable onto the stack at the beginning of a scope, and restore the previous one at the end.
Any flags raised during the scope must be manually checked, handled, and cleared before the end, otherwise a debugging assert will be triggered to warn that restoring the old IntFlags.local value would cause a loss of information.