| Last Modified On : | December 14, 2008 5:24 PM PST |
Rate |
|
Cause:
Overidden virtual functions should have exception-specification at least as restrictive as its own. A caller function might not be able to catch if derived threw an exception that was not advertised in the base class function.
For Example
class Base{
public:
virtual void foo() throw(){} // implies function do not throw any exception
virtual void foo1() throw(int,char){}
virtual void foo2() throw(int){}
};
class Derived:public Base{
public:
void foo() {} // Not ok, Any kind of exception can be thrown. No restriction.
void foo1() throw (int){} //ok, Derived::foo1 is more restrictive than Base::foo1
void foo2() throw(int, char){} // not ok, Derived::foo is less restrictive than Base::foo
};
Example:
Intel compiler emits this diagnostic for following type of code.
class Base{
public:
virtual ~Base() throw(){}
};
class Derived:public Base{ // generates 811. Implicit Destructor do not have any exception specification.
};
Default type of this diagnostic is warning.
>icl /c /EHsc test.cpp
Intel(R) C++ Compiler Professional for applications running on IA-32, Version 11
.0 Build 20080916 Package ID: w_cproc_p_11.0.047
Copyright (C) 1985-2008 Intel Corporation. All rights reserved.test.cpp
test.cpp(6): warning #811: exception specification for implicitly declared virtu
al function "Derived::~Derived" is incompatible with that of overridden function
"Base::~Base"
class Derived:public Base{ // generates 811. Implicit Destructor do not have any exceptin specification.
^
Destructor in the derived class is implicit and is generated by compiler. The generated destructor having no exception specification implies that it can throw any kind of exception. So compiler warns about such code.
Possible fix for this diagnostic is to change the code so that the derived functions exception specification is as restrictive as or more restrictive than base function.
To get rid of the warning, above given code can be re-written as:
class Base{
public:
virtual ~Base() throw(){}
};
class Derived:public Base{
public:
~Derived() throw () { }
};

English | 中文 | Русский | Français
Grishma Kotecha (Intel)
| ||
Jennifer Jiang (Intel)
|