q0987
q0987

Reputation: 35982

C++ Exception Specification for overloaded functions

class ESClass
{
public:
    void PrintMe() throw();
    void PrintMe(int) throw(int);
};

I want to know whether or not we can define different exception specification for overloaded functions. In other words, can we give different exception specification to different version of the function PrintMe?

Note from VS2010:

warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)

Upvotes: 4

Views: 3434

Answers (1)

James McNellis
James McNellis

Reputation: 355079

Yes: they are different functions, they can have different exception specifications.

If a virtual member function has an exception specification, any override (not overload) must have an exception specification that is at least as strict as the member function being overridden.

Of course, you should "never write an exception specification" except in those few situations where you must.

Visual C++ does not fully support exception specifications, so it allows some code that is not actually valid per the C++ language specification. The warning you mention just means that you are using code that uses a C++ language feature not supported by Visual C++:

A function is declared using exception specification, which Visual C++ accepts but does not implement. Code with exception specifications that are ignored during compilation may need to be recompiled and linked to be reused in future versions supporting exception specifications.

Upvotes: 9

Related Questions