https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62063
Bug ID: 62063 Summary: g++ disregards template specialization and skips try/catch blocks Product: gcc Version: 4.9.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: ferenc.geczi at ericsson dot com Created attachment 33276 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33276&action=edit Source code for reproducing the reported issue g++ disregards that template specialization might happen in a different compilation unit. When compiling the attached example: With -O0, it eliminates a catch block since it can instantiate a template resulting a non throwing method, but disregards the specialization in which an exception is actually thrown. Adding the '-fnon-call-exceptions' flag this doesn't happen, but that flag shouldn't be required. With -O2 and -O3 even the try block gets eliminated, but in that case the '-fnon-call-exceptions' doesn't help. Please see attachment: $ g++-4.9 -O0 -Wall Client.cpp Test.cpp main.cpp && ./a.out Throwing exception terminate called after throwing an instance of 'int' Aborted $ g++-4.9 -O0 -Wall -fnon-call-exceptions Client.cpp Test.cpp main.cpp && ./a.out Throwing exception Exception handled! END $ g++-4.9 -O2 -Wall Client.cpp Test.cpp main.cpp && ./a.out END $ g++-4.9 -O3 -Wall Client.cpp Test.cpp main.cpp && ./a.out END $ g++-4.9 -O2 -Wall -fnon-call-exceptions Client.cpp Test.cpp main.cpp && ./a.out END $ g++-4.9 -O3 -Wall -fnon-call-exceptions Client.cpp Test.cpp main.cpp && ./a.out END For comparison, clang++ works as expected with -O0, without the -fnon-callexceptions. Although it has the same the behavior with higher optimization levels.