|
-
June 19th, 2013, 10:08 AM
#1
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.
-
June 19th, 2013, 03:34 PM
#2
Re: A question regarding exception specification
 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
-
June 19th, 2013, 04:26 PM
#3
Re: A question regarding exception specification
 Originally Posted by Paul McKenzie
It is good to know. Thanks.
-
June 20th, 2013, 06:53 AM
#4
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.
-
June 20th, 2013, 11:44 AM
#5
Re: A question regarding exception specification
 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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|