„Google“ grupės nebepalaiko naujų „Usenet“ įrašų ar prenumeratų. Istorinį turinį galima peržiūrėti.

g++ error: looser throw specifier

744 peržiūros
Praleisti ir pereiti prie pirmo neskaityto pranešimo

Ralph Peterson

neskaityta,
2003-02-20 14:36:382003-02-20
kam:
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

neskaityta,
2003-02-21 00:16:242003-02-21
kam:
"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

neskaityta,
2003-02-21 01:03:502003-02-21
kam:

(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 naujų pranešimų