Reputation: 35982
Based on this
Incidentally, you can do this kind of assignment of a pointer to a function as long as the target’s exception specification is no more restrictive than the source’s
class A{};
class B{};
class C{};
void f() throw(A,B,C) {}
void (*pf)() throw(A,B);
int main()
{
pf = f; // pf is more restrictive than that of f. I expect an error here!
}
The last statement should not pass the compiler. However, I have tried VS2010 and GCC latest version. Neither or them complain about it.
Question> Why?
Upvotes: 0
Views: 104
Reputation: 500337
It's a long-standing bug in g++: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12255
The underlying issue is that G++ just ignores exception-specifications on non-function declarations. I think this choice originated in the uncertainty about whether or not the exeception-specification would become part of a function type rather than remain associated with the declaration.
It wouldn't surprise me if VS was similar in this regard.
Upvotes: 1
Reputation: 40603
This looks like a bug in GCC and VS2010. Clang won't compile it, and it should not compile according to [except.spec]/5
:
... A similar restriction applies to assignment to and initialization of pointers to functions, pointers to member functions, and references to functions: the target entity shall allow at least the exceptions allowed by the source value in the assignment or initialization.
The standard also includes the following example:
class A { /*...*/ };
void (*pf1)(); // no exception specification
void (*pf2)() throw(A);
void f() {
pf1 = pf2; // OK: pf1 is less restrictive
pf2 = pf1; // error: pf2 is more restrictive
}
Upvotes: 1