The compiler cannot normally know that there is a memory leak.
Once the code returns from the constructor of Test
, the
compiler must assume that the object has been fully and
correctly constructed, which could (and often does) mean that
the constructor has registered a pointer to it somewhere, and
that other code will expect to find it.
The standard could have specified that an exception will cause
delete
to be called on all objects newed in the complete
expression it passes through. There are a number of historical
reasons why this wasn't even considered. And today, it would
probably break too much existing code.
The standard also could have imposed a strict ordering on the evaluation of an expression. This would remove a lot of issues of undefined behavior, and make code more deterministic (so tests are more reliable). Historically, C didn't take this route because of its impact on optimization; C++ has kept the rule.