Exiting from the Middle of an Expression Without Using Exceptions
Posted
by Jon Purdy
on Stack Overflow
See other posts from Stack Overflow
or by Jon Purdy
Published on 2010-04-21T12:13:08Z
Indexed on
2010/04/21
13:03 UTC
Read the original article
Hit count: 313
Is there a way to emulate the use of flow-control constructs in the middle of an expression? Is it possible, in a comma-delimited expression x, y
, for y
to cause a return
?
Edit: I'm working on a compiler for something rather similar to a functional language, and the target language is C++. Everything is an expression in the source language, and the sanest, simplest translation to the destination language leaves as many things expressions as possible. Basically, semicolons in the target language become C++ commas. In-language flow-control constructs have presented no problems thus far; it's only return
. I just need a way to prematurely exit a comma-delimited expression, and I'd prefer not to use exceptions unless someone can show me that they don't have excessive overhead in this situation.
The problem of course is that most flow-control constructs are not legal expressions in C++. The only solution I've found so far is something like this:
try {
return
x(), // x();
(1 ? throw Return(0) : 0); // return 0;
} catch (Return& ret) {
return ref.value;
}
The return
statement is always there (in the event that a Return
construct is not reached), and as such the throw
has to be wrapped in ?:
to get the compiler to shut up about its void
result being used in an expression.
I would really like to avoid using exceptions for flow control, unless in this case it can be shown that no particular overhead is incurred; does throwing an exception cause unwinding or anything here? This code needs to run with reasonable efficiency. I just need a function-level equivalent of exit()
.
© Stack Overflow or respective owner