Boost logo

Boost :

Subject: Re: [boost] Interest in breakable labeled scope emulation?
From: Alex Perry (Alex.Perry_at_[hidden])
Date: 2013-01-14 08:05:54


On 14 January 2013 10:56 Rob Stewart [mailto:robertstewart_at_[hidden]] wrote :-

>
> While I understand the value of being able to exit such compound scopes
> cleanly, goto is a poor solution as it bypasses destructors, which makes this
> idea error prone.
>

Really? - that's rather worrying - especially for RAII idiom

I don't use goto very often but I know it exists at various points in our code base (+ lots of RAII classes) was under the impression that destructors were handled correctly with goto - am I wrong?

I just tried :-

#include <iostream>

struct A
{
        ~A() {std::cout << "destructor called\n"; }
};

int main( int argc, char** argv )
{
        std::cout << "start 1st scope\n";
        {
                A a;
        }
        std::cout << "end 1st scope\n";
        std::cout << "start 2nd scope\n";
        {
                A a;
                goto end_scope2;
        }
end_scope2:
        std::cout << "end 2nd scope\n";
        std::cout << "start 3rd scope\n";
        if ( false )
        {
end_scope3:;
        }
        else
        {
                A a;
                goto end_scope3;
        }
        std::cout << "end 3rd scope\n";
        return 0;
}

And got

start 1st scope
destructor called
end 1st scope
start 2nd scope
destructor called
end 2nd scope
start 3rd scope
destructor called
end 3rd scope

for (Windows 7)
C:\Workspace\test>cl --version
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

And (Centos 6)
[]$ g++ --version
g++ (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)

So seems to call the destructor in the correct place.

Alex


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk