Code:
#include<iostream>
using namespace std;
void foo() throw(char) {throw 'a';}
int main() try {
void (*pf)() throw(float);
pf = foo; // This should NOT work
pf();
}
catch(const char& c){cout << "Catched ::> " << c << endl;}
Why it is possible to pass foo to pf even though the foo exception specification is different from what the function pointer pf has ? Is this a bug in my compiler?
Exception specifications don’t participate in a function’s type.
Correction:
As pointed out in other answer, it is indeed a compiler bug. It is well known fact that most compilers are buggy in implementing exception specifications. Also, they are deprecated in C++11. So,
Follow Herb Sutter’s advice with exception specifications: