MSVC Error-C2694

03 Jan 2022

Compiler Error C2694:

Error Description:

Overriding virtual function has less restrictive exception specification than base class virtual member funcion base.

Error Details:

Derived class is overriding some virtual function of the base class with lesser exception specification.

Error Concept:

Whenever any virtual function is declared with other properties related to the exceptions then it is expected that the Derived class implementing the base class virtual function should specify same exception as that of the Base class virtual function.

Compiler Flag:

/Za: If this flag is enable then it would allow the Microsoft specific extensions of the ISO C89/C90 which are not compatible with ISO standards. It means enabling such type of flag will actually won’t give compiler error because the code is fine as per the extended standards of microsoft. The risk is that the same code will give compiler error else-where because the style of coding is not compatible with ISO C89/C90.

Error Situations:

Let us have base class Base with as below.

class Base {
public:
virtual void f(void) throw(int) { 
// this type of function definition is telling the compiler that this function could throw error of type integer.
}
}; 

class Derived : public Base {
public:
void f(void) throw(...){} 
// here in ths definition the throw is not specifying the exception type. Thus compiler will emit error here.
// void f(void) throw(int){} This definition is correct way to define in base class
};

Since C++11 standards the exception declaration in function is given by noexcept as below:

class Base {
protected:
virtual void display(void) noexcept;
};
class Derived:public Base {
void display(void) noexcept {// some definition}
};

Addition Information:

noexcept :The noexcept specification is not a part of function type. It can only appear as lambda declarator or top level function declaration when declaring functions. This could also appear as part of declaration for variables, non-static data members which are of type function, pointer to function or reference to function or pointer to member function. This may also appear as argument to function of type function.

void f() noexcept; // function f doesn't throw any exceptions
void (*fp)() noexcept(false); 
// fp a pointer to function with no arguments is actually specifying that 
//the function pointed by this function pointer can throw exception.

Additional Rules:


Comments

Join the discussion for this article on this ticket. Comments appear on this page instantly.