Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Exception Specification for overloaded functions

Tags:

c++

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)

like image 779
q0987 Avatar asked Jan 19 '23 00:01

q0987


1 Answers

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.

like image 200
James McNellis Avatar answered Jan 30 '23 06:01

James McNellis