https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87613
Bug ID: 87613
Summary: Non-reachable default required in switch statement to
get optimal code
Product: gcc
Version: 7.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: chtz at informatik dot uni-bremen.de
Target Milestone: ---
Created attachment 44841
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44841&action=edit
Sample code with non-reachable default case
Compiling the attached code with -O2 generates a jump-table, which directly
contains the `foo<int>()` function addresses. Removing the (non-reachable)
`default` case generates an indirect jump-table which jumps to code that first
moves the function address into a register and then jumps.
In g++-8.x this always generates optimal code, but without the default-case a
warning is generated:
warning: control reaches end of non-void function [-Wreturn-type]
Godbolt-Link (try around with commenting in/out the default or return 0;)
https://godbolt.org/z/itpvL6
Directly returning writing this also leads to an indirect jump-table:
case X: return foo<X>();