fbrereto
fbrereto

Reputation: 35935

Can Clang warn me when I might throw an exception from a `noexcept` destructor?

C++11 specifies destructors as noexcept by default. Is there a way I can get Clang to report cases where my noexcept destructors might throw an exception (and hence call std::terminate)?

Upvotes: 1

Views: 775

Answers (1)

Deduplicator
Deduplicator

Reputation: 45684

First, C++ does not specify destructors as noexcept by default.
It specifies them as noexcept(all subobjects destructors are noexcept).

Next, we can categorize expressions and statements in one category each of:

  1. Never returns, may return, always returns.
  2. Never throws, may throw, always throws.

In all of them, only the first case will be marked as exceptional.

In order to give good results, the compiler must be able to analyze the program behavior sufficiently to determine whether for any possible state, a throwing expression is ever executed.

If you are happy with far too many warnings about impossible scenarios, you can get a result here.
Conversely, if you only want to warn about blatant cases, you might also get some results.

Trouble is, all the interesting cases devolve to solving the halting problem.
And no, we are no further along with it.

Upvotes: 1

Related Questions