Exclusion specifications are incompatible in the declaration and in the implementation of the function

We have the following code

int main() { void f() throw(int); f(); return 0; } void f() { } 

GCC and clang compile it well. But in the standard there is such an item:

n3376 15.4 / 4

If any function declaration has an exception specification that is not a noexcept specification that allows all exceptions, all declarations, including a definition and any explicit specialization, of this function must have a compatible exception specification .

And for the following example: gcc - error, clang - warning

 void f() throw(int); int main() { f(); return 0; } void f() { } 

Why is there a difference in these fragments? Thanks.

+6
source share
1 answer

n3376 15.4 / 4 of std indicates that all decorations and function definitions must have the same cast type. Here:

 void f() throw(int); int main() { f(); return 0; } void f() { } 

declaration void f() throw(int); , and the definition of void f() { } is global. Thus, they are in conflict because the declaration is for a function that throws an int, while the definition refers to a function without a throw specification.

Now, when you place an ad in the main scop, the definition is not in the same scop, during this scop the definition is unknown, so you can compile.

I hope you understand my English, sorry.

+2
source

All Articles