Grupos de Google ya no admite publicaciones ni suscripciones nuevas de Usenet. El contenido anterior sigue visible.

g++ error: looser throw specifier

746 vistas
Ir al primer mensaje no leído

Ralph Peterson

no leída,
20 feb 2003, 2:36:38 p.m.20/2/2003
para
Hi,

I wrote an exception class that's derived from std::logic_error:

#include <stdexcept>
#include <string>

class my_error: public std::logic_error
{
public:

my_error(const std::string& what_arg):
std::logic_error(what_arg)
{
}
};

This compiles fine. Now I add a std::string member variable:

class my_error: public std::logic_error
{ // <- line 5
public:

my_error(const std::string& what_arg):
std::logic_error(what_arg)
{
}

private:

std::string foo_;
};

Now g++ 3.2 (windows version) tells me:

my_error.cpp:5: looser throw specifier for `virtual my_error::~my_error()'
E:/gcc3/include/c++/3.2/stdexcept:64: overriding `virtual
std::logic_error::~logic_error() throw ()'

Can anybody tell me what I did wrong?

TIA
Ralph

Victor Bazarov

no leída,
21 feb 2003, 12:16:24 a.m.21/2/2003
para
"Ralph Peterson" <ralph.p...@gmx.net> wrote...


My guess would be that the compiler is trying to tell you that
the existence of the 'foo_' member may cause the destructor to
throw (when it deallocates the 'foo_'). The d-tor of the class
basic_string does not have any exception specification. That
means it _may_ throw something. Throwing something would violate
the "promise" the 'logic_error' makes _not_to_throw_ from the
destructor. You need to use a char array or a special string
class, deallocation of which _guarantees_ not to throw by having
the 'throw()' specification in its d-tor.

Victor
--
Please remove capital A's from my address when replying by mail


Alan Gutierrez

no leída,
21 feb 2003, 1:03:50 a.m.21/2/2003
para

(Newsreader troubles, sorry if this is dupicate)

When you added the std::string to my_error, gcc generated a default
destructor for you. The generated destructor has no throw specs.
The destructor for std::logic_error has the throw spec throw(),
which means throw nothing. The compiler generated destructor is
looser (throw anything) and so gcc complains.

To solve the problem, define a destructor with the appropriate throw
specs.

This compiles fine. Now I add a std::string member variable:

class my_error: public std::logic_error
{
public:

my_error(const std::string& what_arg):
std::logic_error(what_arg)
{
}

~my_error () throw () { } // should work

private:

std::string foo_;
};

--
Alan Gutierrez - ajg...@izzy.net
http://khtml-win32.sourceforge.net/ - KHTML on Windows

0 mensajes nuevos