Re: How expensive are exceptions?
Sergey P. Derevyago wrote:
In the case of array construction you also need the
number of elements so far created.
But that number is available, and can be retrieved using the PC
value as a guide. This issue has come up over and over, and has
been explained again and again, and somehow it's never enough.
It's really very simple. As the array is being constructed, the
code is somehow tracking how many elements have been done. It
could be using a loop and a register counter, or a memory counter,
or the entire construction can be unrolled into a series of code.
It doesn't matter. The unwind code for that PC is written to know
how to construction code is keeping track, and can therefore do the
destruction of the right number of elements.
At least in the case of array construction compiler must
take an additional effort to make the number of elements so far created
accessible for the EH unwinder. This effort kills certain local
optimizations.
There is no additional effort. The EH code for each PC is wriiten
to use whatever facility the construction code is using.
Perhaps an example could make you understand? Suppose we have the
following code:
extern void f(); // potential thrower
struct a { a() { f(); } ~a() { } };
int main() { a as[5]; }
The compiler decides to unroll the construction loop, so it looks
like this:
a *p = stackalloc(5 * sizeof(a));
L0: new(&p[0]) a;
L1: new(&p[1]) a;
L2: new(&p[2]) a;
L3: new(&p[3]) a;
L4: new(&p[4]) a;
Notice that there's no counter at all, because the loop is unrolled
and the number of elements is known at compile time. Each of the calls
to a's constructor can throw, so there has to be a handler for each.
The handlers look like this (and fall through):
Handler_When_L4_Throws: p[3].~a();
Handler_When_L3_Throws: p[2].~a();
Handler_When_L2_Throws: p[1].~a();
Handler_When_L1_Throws: p[0].~a();
Handler_When_L0_Throws: // keep unwinding...
The unwind code uses the PC, namely one of L0-L4, to select the place
to go to do the cleanup. As you should see, no counter needs to be kept.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]