ForEveR
ForEveR

Reputation: 55897

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.

Upvotes: 6

Views: 3584

Answers (1)

Hulor
Hulor

Reputation: 229

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.

Upvotes: 3

Related Questions