On Mon, 16 May 2022 at 08:33, Sebastian Wittmeier <wittmeier@projectalpha.org> wrote:

But with a nonlinear program flow (including simple loops or conditionals), this can get really problematic.


void f() {

    string s1, s2, s3;

    if (flag)

        s2 = reloc s1; // removes s1 from lexical scope


        s3 = reloc s1; // !!! compilation error


The rule is that if a variable is removed within a substatement of an if statement, and is not explicitly removed within the other substatement, then it is implicitly removed *at the end* of the other substatement (calling its destructor, if appropriate).

As for loops, it is ill-formed to jump over such an expression, in the same way as it is ill-formed to jump over the initialization of an automatic variable; so within a loop you can only reloc automatic variables declared within that loop.