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:
Moral #1: Never write an exception specification.
Moral #2: Except possibly an empty one, but if I were you I’d avoid even that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With