Bug 103092 - Non-throwing function pointer can point to a throwing-function in C++14
Summary: Non-throwing function pointer can point to a throwing-function in C++14
Status: RESOLVED DUPLICATE of bug 49332
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: 12255
  Show dependency treegraph
 
Reported: 2021-11-05 08:37 UTC by Fedor Chelnokov
Modified: 2021-11-06 04:55 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-11-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fedor Chelnokov 2021-11-05 08:37:44 UTC
This program
```
void func(){}

int main() {
    void(*pFn)() noexcept(true);
    pFn = func;
    (void)pFn;
}

```
is accepted by GCC in C++14 mode (but not by Clang), demo: https://gcc.godbolt.org/z/a3EjxcKvz

Starting from C++17, GCC also rejects it. Could you please verify C++14 mode here?

Related discussion: https://stackoverflow.com/q/66574353/7325599
Comment 1 Jonathan Wakely 2021-11-05 09:20:14 UTC
The relevant rule before C++17 is [except.spec] p5

"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."
Comment 2 Jonathan Wakely 2021-11-05 09:26:27 UTC
G++ does not give an error for the example in p5:

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
}

That should be ill-formed in C++98/11/14.

The equivalent with a noexcept-specifier would be:

class A { /* ... */ };
void (*pf1)(); // no exception specification
void (*pf2)() noexcept;

void f() {
  pf1 = pf2; // OK: pf1 is less restrictive
  pf2 = pf1; // error: pf2 is more restrictive
}

G++ doesn't reject this in 98/11/14, only C++17 and up.
Comment 3 Andrew Pinski 2021-11-05 22:45:53 UTC
(In reply to Jonathan Wakely from comment #2)
> G++ does not give an error for the example in p5:
> 
> 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
> }
> 
> That should be ill-formed in C++98/11/14.

The above testcase is PR 12255.
Comment 4 Jonathan Wakely 2021-11-06 00:28:45 UTC
Ah yes, and I think this is a dup of pr 49332
Comment 5 Andrew Pinski 2021-11-06 04:55:26 UTC
Dup of bug 49332.

*** This bug has been marked as a duplicate of bug 49332 ***