QCC/g++ 2.95.3 template function try block support?

Hi All:

I am running NC 6.2.1 with QCC/gcc version 2.95.3. I am trying to get
function try blocks on constructors to work on templated classes.

A function try block for a non templated class would look like:

buffer(size_t const count) throw(bad_alloc)
try
: p(new char[count])
{ /constructor body/ }
catch (exception & e)
{
//do something to catch exception
}

This will compile and work correctly under 2.95.3 and throw a
constructor exception (if the “new” fails) which will be caught by the
constructor. The templated version of this snippet is:

template
class Tbuffer
{
public:
Tbuffer(size_t const count) throw(bad_alloc)
try
: p(new T[count])
{ /constructor body/ }
catch (exception & e)
{
//do something
}

The templated code will also compile, but will not catch the thrown
exception. Any idea what may be going on? Is this just a problem with
2.95.3 specification compliance or do I need to set some other flag?

The complete test code, compile command and results are below.

Thanks in advance for the assistance.
Regards,
–Jeff Strickrott

====================
compile with “QCC -D_PTHREADS=1 test.cpp -o btest”

#include
#include
#include
#include
using namespace std;

class buffer
{
public:

buffer(size_t const count) throw(bad_alloc)
try
: p(new char[count])
{
//throw bad_alloc();
cout << “buffer(size_t const count)” << endl;
}
catch (exception & e)
{
cerr << “caught exception " << e.what( ) << " in buffer”
<< endl;
cerr << "Type " << typeid( e ).name( ) << endl;
exit(1);
}

~buffer() throw()
{
delete[] p;
}

private:
char *const p;
};


//==================================================
//
template
class Tbuffer
{
public:
Tbuffer(size_t const count) throw(bad_alloc)
try
: p(new T[count])
{
//throw bad_alloc();
cout << “Tbuffer<” << typeid( T ).name() <<
“>::Tbuffer(size_t const count)”<< endl;
}
catch (exception & e)
{
cerr << “caught exception " << e.what( ) << " in Tbuffer”
<<endl;
cerr << "Type " << typeid( e ).name( ) << endl;
exit(1);
}

~Tbuffer() throw()
{
cout << “~Tbuffer()” << endl;
delete[] p;
}
private:
T *const p;
};


void main()
{
cout << “test.cpp” << endl;
try
{
Tbuffer Tb(-1); //will throw bad_alloc, but it is not caught
by Tbuffer
//Tbuffer Tb(1);

buffer b(11);
//buffer b(-11); //will throw and catch bad_alloc exception
//that is caught by buffer
}
catch(…)
{
cerr << “caught some exception” << endl;
}
}

//results when buffer throws exception

./btest

test.cpp
Tbuffer::Tbuffer(size_t const count)
caught exception bad_alloc in buffer
Type 9bad_alloc


//results when Tbuffer throws exception

./btest

test.cpp
caught some exception

Appears to be a known bug in 2.95. Your example
and the following work with gcc 3.3.1

(gcc/testsuite/g++.old-deja/g++.pt/fntry1.C)
// Bug: g++ silently ignores function-try-blocks in templates.
// Submitted by Jason Merrill <jason@cygnus.com>

template void f (T) try { throw 1; } catch (…) { }

int main ()
{
f (1);
}


Jeff Strickrott wrote:

Hi All:

I am running NC 6.2.1 with QCC/gcc version 2.95.3. I am trying to get
function try blocks on constructors to work on templated classes.

A function try block for a non templated class would look like:

buffer(size_t const count) throw(bad_alloc)
try
: p(new char[count])
{ /constructor body/ }
catch (exception & e)
{
//do something to catch exception
}

This will compile and work correctly under 2.95.3 and throw a
constructor exception (if the “new” fails) which will be caught by the
constructor. The templated version of this snippet is:

template <class T
class Tbuffer
{
public:
Tbuffer(size_t const count) throw(bad_alloc)
try
: p(new T[count])
{ /constructor body/ }
catch (exception & e)
{
//do something
}
code cut…

The templated code will also compile, but will not catch the thrown
exception. Any idea what may be going on? Is this just a problem with
2.95.3 specification compliance or do I need to set some other flag?

The complete test code, compile command and results are below.

Thanks in advance for the assistance.
Regards,
–Jeff Strickrott

====================
compile with “QCC -D_PTHREADS=1 test.cpp -o btest”

#include <exception
#include <iostream
#include <typeinfo
#include<new
using namespace std;

class buffer
{
public:

buffer(size_t const count) throw(bad_alloc)
try
: p(new char[count])
{
//throw bad_alloc();
cout << “buffer(size_t const count)” << endl;
}
catch (exception & e)
{
cerr << “caught exception " << e.what( ) << " in buffer”
endl;
cerr << "Type " << typeid( e ).name( ) << endl;
exit(1);
}

~buffer() throw()
{
delete[] p;
}

private:
char *const p;
};


//==================================================
//
template <class T
class Tbuffer
{
public:
Tbuffer(size_t const count) throw(bad_alloc)
try
: p(new T[count])
{
//throw bad_alloc();
cout << “Tbuffer<” << typeid( T ).name()
“>::Tbuffer(size_t const count)”<< endl;
}
catch (exception & e)
{
cerr << “caught exception " << e.what( ) << " in Tbuffer”
endl;
cerr << "Type " << typeid( e ).name( ) << endl;
exit(1);
}

~Tbuffer() throw()
{
cout << “~Tbuffer()” << endl;
delete[] p;
}
private:
T *const p;
};


void main()
{
cout << “test.cpp” << endl;
try
{
Tbuffer Tb(-1); //will throw bad_alloc, but it is not caught
by Tbuffer
//Tbuffer Tb(1);

buffer b(11);
//buffer b(-11); //will throw and catch bad_alloc exception
//that is caught by buffer
}
catch(…)
{
cerr << “caught some exception” << endl;
}
}

//results when buffer throws exception

./btest

test.cpp
Tbuffer::Tbuffer(size_t const count)
caught exception bad_alloc in buffer
Type 9bad_alloc


//results when Tbuffer throws exception

./btest

test.cpp
caught some exception

Thanks Gary.

Any suggestions on the easiest least painful way to upgrade QNX 6.2.1
environment to gcc 3.3.1 without breaking anything?

I usually use QCC to compile as this causes less problems then using
gcc/g++.

Regards,
–Jeff Strickrott

Garry Turcotte wrote:

Appears to be a known bug in 2.95. Your example
and the following work with gcc 3.3.1

(gcc/testsuite/g++.old-deja/g++.pt/fntry1.C)
// Bug: g++ silently ignores function-try-blocks in templates.
// Submitted by Jason Merrill <> jason@cygnus.com

template void f (T) try { throw 1; } catch (…) { }

int main ()
{
f (1);
}


Jeff Strickrott wrote:

Hi All:

I am running NC 6.2.1 with QCC/gcc version 2.95.3. I am trying to
get function try blocks on constructors to work on templated classes.

A function try block for a non templated class would look like:

buffer(size_t const count) throw(bad_alloc)
try
: p(new char[count])
{ /constructor body/ }
catch (exception & e)
{
//do something to catch exception
}

This will compile and work correctly under 2.95.3 and throw a
constructor exception (if the “new” fails) which will be caught by
the constructor. The templated version of this snippet is:

template <class T
class Tbuffer
{
public:
Tbuffer(size_t const count) throw(bad_alloc)
try
: p(new T[count])
{ /constructor body/ }
catch (exception & e)
{
//do something
}
code cut…

The templated code will also compile, but will not catch the thrown
exception. Any idea what may be going on? Is this just a problem with
2.95.3 specification compliance or do I need to set some other flag?

The complete test code, compile command and results are below.

Thanks in advance for the assistance.
Regards,
–Jeff Strickrott

====================
compile with “QCC -D_PTHREADS=1 test.cpp -o btest”

#include <exception
#include <iostream
#include <typeinfo
#include<new
using namespace std;

class buffer
{
public:

buffer(size_t const count) throw(bad_alloc)
try
: p(new char[count])
{
//throw bad_alloc();
cout << “buffer(size_t const count)” << endl;
}
catch (exception & e)
{
cerr << “caught exception " << e.what( ) << " in
buffer” << endl;
cerr << "Type " << typeid( e ).name( ) << endl;
exit(1);
}

~buffer() throw()
{
delete[] p;
}

private:
char *const p;
};


//==================================================
//
template <class T
class Tbuffer
{
public:
Tbuffer(size_t const count) throw(bad_alloc)
try
: p(new T[count])
{
//throw bad_alloc();
cout << “Tbuffer<” << typeid( T ).name()
“>::Tbuffer(size_t const count)”<< endl;
}
catch (exception & e)
{
cerr << “caught exception " << e.what( ) << " in
Tbuffer” <<endl;
cerr << "Type " << typeid( e ).name( ) << endl;
exit(1);
}

~Tbuffer() throw()
{
cout << “~Tbuffer()” << endl;
delete[] p;
}
private:
T *const p;
};


void main()
{
cout << “test.cpp” << endl;
try
{
Tbuffer Tb(-1); //will throw bad_alloc, but it is not
caught by Tbuffer
//Tbuffer Tb(1);

buffer b(11);
//buffer b(-11); //will throw and catch bad_alloc exception
//that is caught by buffer
}
catch(…)
{
cerr << “caught some exception” << endl;
}
}

//results when buffer throws exception

./btest

test.cpp
Tbuffer::Tbuffer(size_t const count)
caught exception bad_alloc in buffer
Type 9bad_alloc


//results when Tbuffer throws exception

./btest

test.cpp
caught some exception

Jeff Strickrott <jstric01@cs.fiu.edu> wrote:
JS > Thanks Gary.

JS > Any suggestions on the easiest least painful way to upgrade QNX 6.2.1
JS > environment to gcc 3.3.1 without breaking anything?

JS > I usually use QCC to compile as this causes less problems then using
JS > gcc/g++.

JS > Regards,
JS > --Jeff Strickrott

QNX 6.3 has been released. gcc 3.3.1 is part of QNX 6.3.

If you have a paid support plan you can download 6.3 from the MyQNX page.
You will need to contact QNX sales for a 6.3 license key, which should be
free if you have a paid support plan.

It would be good to see QNX as a supported target for the
current version of GCC. The QNX-specific fixes should be
checked back into the GCC base, so that QNX-specific versions
can be built from the general GCC sources. This would improve
compiler support, as users could report compiler problems
under QNX to the GCC maintainers.

After all, GCC is licensed under the General Public License
of the Free Software Foundation. Source for modified versions
must be released on request. See
http://www.gnu.org/copyleft/gpl.html”, paragraphs
2b and 3b, for QSSL’s obligations.

On a related note, VxWorks 5.5 is now supported by
the stock GCC. See

http://gcc.gnu.org/install/specific.html

So the competition has checked their version back into
the base GCC.

Think of it as easing migration from Linux to QNX.
Thanks.

John Nagle
Team Overbot

Bill Caroselli wrote:

Jeff Strickrott <> jstric01@cs.fiu.edu> > wrote:
JS > Thanks Gary.

JS > Any suggestions on the easiest least painful way to upgrade QNX 6.2.1
JS > environment to gcc 3.3.1 without breaking anything?

JS > I usually use QCC to compile as this causes less problems then using
JS > gcc/g++.

JS > Regards,
JS > --Jeff Strickrott

QNX 6.3 has been released. gcc 3.3.1 is part of QNX 6.3.

If you have a paid support plan you can download 6.3 from the MyQNX page.
You will need to contact QNX sales for a 6.3 license key, which should be
free if you have a paid support plan.

John Nagle <nagle@downside.com> wrote:

It would be good to see QNX as a supported target for the
current version of GCC. The QNX-specific fixes should be
checked back into the GCC base, so that QNX-specific versions
can be built from the general GCC sources. This would improve
compiler support, as users could report compiler problems
under QNX to the GCC maintainers.

It’s already on going - we have committers on both gdb and gcc projects.

chris


Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/