Re: Suggested extention of the break statement
Gerard J. Cerchio wrote:
First of all, try to in general use RAII (objects with proper
destructors,
handling clean-up in destructors) rather than try-catch.
And second, it seems that you're addressing the case of trying to do all
too much in one function, for which it's usually a better solution to
break that large function up into smaller, more focused functions.
That said,
// B
#define BEGIN_BREAKABLE for(;;){
#define END_BREAKABLE break;}
I'd do it this way:
#define BEGIN_BREAKABLE do {
#define END_BREAKABLE } while (false);
The rationale is that a bogus "continue" statement will not repeat the
try block in this case.
int bar()
{
int result = 1;
BEGIN_BREAKABLE
try
{
// Some code
break;
// More code
result = 2;
}
catch( ... ) { std::terminate(); }
END_BREAKABLE
return result;
}
This is precisely what I recommend. It allows local method cleanup
with a simple break syntax. If try allows break, then 4 lines are removed
from your example. Your defines would probably be some distance away
from their instantiations making the code harder to read. My suggestion
is all about keeping the code easy to scan with the eye.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]