Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception specifications are not compatible in declaration and in realisation of function

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.

like image 345
ForEveR Avatar asked Feb 19 '13 05:02

ForEveR


People also ask

What is an exception specification when it is used?

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.

What is Noexcept keyword in C++?

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.


1 Answers

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.

like image 199
Hulor Avatar answered Oct 30 '22 03:10

Hulor