https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92640

            Bug ID: 92640
           Summary: Incorrect warning: exception of type 'const derived&'
                    will be caught by earlier handler for 'const base&'
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jorrit at jorrit dot de
  Target Milestone: ---

In the following program, the catch-clause for `derived` is taken, since
`derived` is derived privately from `base`.  However, the compiler still emits
a warning that the catch clause for `derived` will nether be taken.

Compiled without any particular options.
> struct base {};
> class derived : base {};
>
> template<class>
> int check()
> {
>   try {
>     throw derived();
>   }
>   catch(const base &)    { return 0; }
>   catch(const derived &) { return 1; }
> }
>
> int main()
> {
>   return check<int>();
> }

Compiler stderr
> <source>: In function 'int check()':
> <source>:11:3: warning: exception of type 'const derived&' will be caught
>    11 |   catch(const derived &) { return 1; }
>       |   ^~~~~
> <source>:10:3: warning:    by earlier handler for 'const base&'
>    10 |   catch(const base &)    { return 0; }
>       |   ^~~~~

The program exits with 1 when run, contradicting the compiler warning.

Could reproduce with the following version on godbolt,
https://godbolt.org/z/jerjHS:
- current trunk: g++ (Compiler-Explorer-Build) 10.0.0 20191122 (experimental)
- g++ (Compiler-Explorer-Build) 9.2.0
- g++ (Compiler-Explorer-Build) 8.3.0
- g++ (Compiler-Explorer-Build) 7.5.0

Note: this may or may not be a dup of Bug 69373.  Though here it is not
(function) pointers, and only happens with the catch clauses inside a template
function.

Reply via email to