Goto around an initialization of a template class

Marco Manfai Yu yumf@ultimatech.com
Fri Dec 18 18:06:00 GMT 1998


Hi,

    I am using egcs-1.1 on i686-pc-linux-gnu.

Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.91.57/specs
gcc version egcs-2.91.57 19980901 (egcs-1.1 release)

The compiler does not give an error message when a templated class is
instantiated with a goto around it. Consider the following code.

--------------------------------------------------------------
#include <iostream.h>

template <typename A>
class G {
public:
    G() { cout << "Constructing G" << endl; }
    ~G() { cout << "Destroying G" << endl; }
};

main ()
{
    goto x;
    G<int> g;          // <---- no complaint
  x:

    return 1;
}
-------------------------------------------------------------

As a result the destructor of g is called even g is not constructed.
This bug only happens when G is a templated class. The following code
get with the right error message.

-------------------------------------------------------------
#include <iostream.h>

class K {
public:
    K() { cout << "Constructing k" << endl; }
    ~K() { cout << "Destroying k" << endl; }
};

main ()
{
    goto x;
    K k;
  x:

    return 1;
}
-------------------------------------------------------------

% g++ -g -o foo foo.cc
foo.cc: In function `int main()':
foo.cc:13: jump to label `x'
foo.cc:11:   from here
foo.cc:12:   crosses initialization of `class K k'



Marco Yu
yumf@ultimatech.com



More information about the Gcc-bugs mailing list