C++ - If an object is declared in a loop, is its destructor called at the end of the loop?

前端 未结 2 678
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 08:59

In C++, an object\'s destructor is called at the closing \"}\" for the block it was created in, right? So this means that if I have:

while(some_condition)
{
             


        
2条回答
  •  独厮守ぢ
    2021-02-07 09:12

    The observable behaviour is that it's called each iteration.

    The usual rules about optimisations still apply though. If the compiler is smart and the object simple then compiler can do anything it likes that still produces the correct behaviour, e.g.:

    #include 
    
    struct foo {
      int i;
      foo() : i (-1) {}
      ~foo() { i = 1; }
    };
    
    int main() {
      int i = 10;
      while (--i) {
        foo f;
        std::cout << f.i;
      }
    }
    

    Compiles to:

    .Ltmp5:
            .cfi_def_cfa_register %rbp
            movl    $_ZSt4cout, %edi
            movl    $-1, %esi
            callq   _ZNSolsEi
            movl    $_ZSt4cout, %edi
            movl    $-1, %esi
            callq   _ZNSolsEi
            movl    $_ZSt4cout, %edi
            movl    $-1, %esi
            callq   _ZNSolsEi
            movl    $_ZSt4cout, %edi
            movl    $-1, %esi
            callq   _ZNSolsEi
            movl    $_ZSt4cout, %edi
            movl    $-1, %esi
            callq   _ZNSolsEi
            movl    $_ZSt4cout, %edi
            movl    $-1, %esi
            callq   _ZNSolsEi
            movl    $_ZSt4cout, %edi
            movl    $-1, %esi
            callq   _ZNSolsEi
            movl    $_ZSt4cout, %edi
            movl    $-1, %esi
            callq   _ZNSolsEi
            movl    $_ZSt4cout, %edi
            movl    $-1, %esi
            callq   _ZNSolsEi
            xorl    %eax, %eax
            popq    %rbp
            ret
    

    I.e. unrolled and no sign of that destructor in there (although the observable behaviour is still the same).

提交回复
热议问题