We have following code
int main()
{
void f() throw(int);
f();
return 0;
}
void f() { }
GCC and clang compiles it well. But, in standard there is such paragraph:
n3376 15.4/4
If any declaration of a function has an exception-specification that is not a noexcept-specification allowing all exceptions, all declarations, including the definition and any explicit specialization, of that function shall have a compatible exception-specification.
And for following example: gcc - error, clang - warning
void f() throw(int);
int main()
{
f();
return 0;
}
void f() { }
Why there is difference in these snippets? Thanks.
Exception specifications are a C++ language feature that indicate the programmer's intent about the exception types that can be propagated by a function. You can specify that a function may or may not exit by an exception by using an exception specification.
C++11 noexcept KeywordThis keyword can be used for specifying that any function cannot throw — or is not ready to throw. Here is a code snippet: void test() noexcept; This declares that test() won't be able to throw.
The n3376 15.4/4 from the std specifie that all déclarations and definitions of a function must have the same throwing type. Here :
void f() throw(int);
int main()
{
f();
return 0;
}
void f() { }
the declaration void f() throw(int);
and the definition void f() { }
are in global scop. So they are in conflict because the declaration is for a function which throw int while the definition is for a function without a throw specification.
Now, when you put the declaration in the main scop, the definition isn't in the same scop, during this scop the definition isn't known so you can compile.
I hope you understood my english, sorry about it.
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