Bug 49027 - g++ ignores -fno-exceptions in uninstantiated template
Summary: g++ ignores -fno-exceptions in uninstantiated template
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-17 17:00 UTC by Ian Lance Taylor
Modified: 2011-05-18 17:36 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Lance Taylor 2011-05-17 17:00:52 UTC
This file compiles without error when using -fno-exceptions:

extern int f2();
template<typename T>
void f1() {
  try { f2(); } catch (...) { }
}

Compiling this very similar file:

extern int f2();
template<typename T>
void f1() {
  try { f2(); } catch (...) { }
}
template f1<int>();

gives these errors:

foo.cc: In function ‘void f1() [with T = int]’:
foo.cc:6:   instantiated from here
foo.cc:4: error: exception handling disabled, use -fexceptions to enable

I think it would be more consistent for g++ to reject uses of try/catch when compiling with -fno-exceptions even in uninstantiated templates.  Otherwise you can get surprising results when code changes: you can find problems in very different pieces of code.
Comment 1 Richard Biener 2011-05-18 09:43:37 UTC
I'm not sure.  What about

extern int f2();
struct NoExcept {};
template<typename T>
void f1() {
  try { f2(); } catch (...) { }
}
template<>
void f1<NoExcept>() {
  f2();
}
template f1<NoExcept>();

?  EH code could be disabled by some tags and specialization.  Especially
for a library that has a non-exception variant that would sound useful.
Comment 2 Andrew Pinski 2011-05-18 17:36:18 UTC
IIRC this was done on purpose but I cannot find the original discussion.