A question regarding exception specification
If a class inherits from another and overrides a virtual function, then the exception-specification for the overridden function must be at least as restrictive. What does it mean? Here is an example,
Code:
class B
{
public:
virtual void foo() throw (int)
{
}
};
class D: public B
{
public:
virtual void foo() throw (double)
{
}
};
It compiles fine. In the derived class, virtual function foo throws double and in the base class, foo throws int. So double is supposed to be at least as restrictive? Thanks.
Re: A question regarding exception specification
Quote:
Originally Posted by
LarryChen
If a class inherits from another and overrides a virtual function, then the exception-specification
Not to cut you off, but exception specifications have been deprecated in the new C++ standard.
Here are two links explaining this:
http://herbsutter.com/2010/03/13/tri...dards-meeting/
http://www.gotw.ca/publications/mill22.htm
So are you going to spend time on a "feature" that hardly anyone used, considered broken, and is now deprecated?
Regards,
Paul McKenzie
Re: A question regarding exception specification
Quote:
Originally Posted by
Paul McKenzie
It is good to know. Thanks.
Re: A question regarding exception specification
not to mention that hardly any compiler even actually implemented it, even if they supported the syntax.
but what the old rule basically said...
if your base class implementation throws X and Y then a derived class implementation can be specified to throw
* nothing
* only X
* only Y
* X and Y
it can't throw something else than those, because any code working off the base class only knows of a possible X and Y being thrown. So an overloaded implementation can't be allowed to throw something the base class doesn't specify.
but as stated, this is deprecated, and even in pre C++11 compilers you'd be hard pressed to find a compiler actually fully implementing it.
Re: A question regarding exception specification
Quote:
Originally Posted by
OReubens
not to mention that hardly any compiler even actually implemented it, even if they supported the syntax.
but what the old rule basically said...
if your base class implementation throws X and Y then a derived class implementation can be specified to throw
* nothing
* only X
* only Y
* X and Y
it can't throw something else than those, because any code working off the base class only knows of a possible X and Y being thrown. So an overloaded implementation can't be allowed to throw something the base class doesn't specify.
but as stated, this is deprecated, and even in pre C++11 compilers you'd be hard pressed to find a compiler actually fully implementing it.
Your explanation is very clear. Thank you very much!