Force inline throwing function in destructor

Viewed 70

If I throw in the destructor of a struct/class and compile with Werror=terminate, the compiler will complain with the following error message:

warning: 'throw' will always call 'terminate' [-Wterminate]
Compiler: GCC 11.1.0

If I however do this the compiler doesn't complain:

#include <iostream>

# define FORCE_INLINE __attribute__((always_inline)) inline

FORCE_INLINE void exception_proxy() {
    throw std::invalid_argument("HI");
}

struct T {
    
    T() = default;
    ~T() {
        exception_proxy();
    }
};

int main()
{
}

Why is this the case? I am trying to understand exactly why the compiler drops the warning when the function is inlined? I understand that the compiler doesn't care to check each function called from a noexcept function but this seems a bit counterintuitive given my knowledge?

Godbolt link to show actual inlining: https://godbolt.org/z/eGW7ov9av

1 Answers

If I throw in the destructor of a struct/class and compile with Werror=terminate, the compiler will complain with the following error message:

Why is this the case?

You get a warning because you throw in a noexcept function.

If I however do this the compiler doesn't complain:

Why is this the case?

You don't get a warning because you don't throw in a noexcept function. You throw in a potentially throwing function.


I am trying to understand exactly why the compiler drops the warning when the function is inlined?

The inlining attribute has no effect on the warning one way or another. You may find that you won't get a warning even if you remove the inlining attribute.

Related