https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79249
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Reduced:
template <typename FnT>
auto attempt(FnT f)
{
return [f]()
{
try
{
f();
}
catch(...)
{
__builtin_puts("caught");
}
};
}
// More complex behavior does not seem to matter here, however
// removing "noexcept" results in GCC generating a program that works as
expected:
void f_ident() noexcept { }
// Even though this is not actually referenced by the other code, removing
// it causes GCC to generate a program that works as expected (the presence of
// "static" seems to make no difference).
auto test_attempt_call_with_exception_handler()
{
static auto fn_ = attempt(f_ident);
return fn_();
}
void g()
{
throw 1;
}
int main()
{
auto f = attempt(g);
f();
}