Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ exception specification in a function pointer

Tags:

c++

exception

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?

like image 919
AlexDan Avatar asked Dec 08 '22 20:12

AlexDan


1 Answers

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.

like image 95
Alok Save Avatar answered Dec 28 '22 22:12

Alok Save