1. Revision history
First revision!2. Introduction
The first priority of this paper is to establish a lasting policy on the usage ofnoexcept
in the standard library that avoids re-litigation for a few years.
The second priority is to establish the author’s preferred policy.
This paper currently recommends the policy that functions that LEWG and LWG agree cannot throw should be marked unconditionally
.
This paper discusses an alternative policy that library functions that could exhibit undefined behavior due to precondition violations should not be marked
(AKA the "Lakos rule", AKA the narrow
policy).
With luck, one of these two policies can gain consensus.
3. Prior noexcept
Discussions
3.1. History papers
-
N2855, Rvalue References and Exception Safety, [GregorN2855]
-
N3248, noexcept Prevents Library Validation., [MeredithN3248]
-
N3279, Conservative use of noexcept in the Library, [MeredithN3279]
-
P0884R0, Extending the noexcept Policy, Rev0, [Josuttis0884]
-
P2920R0, Library Evolution Leadership’s Understanding of the Noexcept Policy History, [Lelbach2920]
3.2. Throws Nothing Rule
3.3. Narrow noexcept
/ Lakos Rule
-
P2831R0, Functions having a narrow contract should not be noexcept, [Doumler2831]
-
P2861R0, The Lakos Rule: Narrow Contracts And noexcept Are Inherently Incompatible, [Lakos2861]
-
P2949R0, Slides for P2861R0: Narrow Contracts and noexcept are Inherently Incompatable, [Lakos2949]
-
P2946R0, A flexible solution to the problems of noexcept, [Halpern2946]
3.4. Both
-
This paper, P3085,
policy for SD-9 (throws nothing)noexcept
4. Policies in question
These two policies only differ on point b.4.1. Common parts (proposed)
This wording matches the shared wording from [Bergé1656] and [Josuttis0884].Instructions to the editor: Please add the following to the "List of Standard Library Policies" in SD-9.
4.1.1.
Rationale reference: P3085policy {#wording_heading}
noexcept
- No library destructor should throw. They shall use the implicitly supplied (non-throwing) exception specification.
- EDITOR’S NOTE: Fill in this option with the selected policy. See below.
- If a library swap function, move-constructor, or move-assignment operator is conditionally-wide (i.e. can be proven to not throw by applying the
operator) then it should be marked as conditionally
noexcept .
noexcept - If a library type has wrapping semantics to transparently provide the same behavior as the underlying type, then default constructor, copy constructor, and copy-assignment operator should be marked as conditionally
the underlying exception specification still holds.
noexcept - No other function should use a conditional
specification.
noexcept - Library functions designed for compatibility with “C” code (such as the atomics facility), may be marked as unconditionally
.
noexcept
4.2. Throws Nothing Rule (proposed)
This wording does not match [Bergé1656], but it has the same intent.b. Each library function that will not throw when called with all preconditions satisfied should be marked as unconditionally
.
noexcept
The wording in [Bergé1656] is as follows:
b. Each library function that LEWG and LWG agree cannot throw, should be marked as unconditionally
.
noexcept
The [Bergé1656] wording could be interpreted as disallowing narrow contract functions, since a function that is called without meeting preconditions could do anything, including throw. That interpretation isn’t the intent of [Bergé1656].
4.3. Narrow noexcept
/ Lakos Rule (not proposed in this revision)
This wording matches the wording in [Josuttis0884].
b. Each library function having a wide contract (i.e., does not specify undefined behavior due to a precondition) that the LWG agree cannot throw, should be marked as unconditionally
.
noexcept
5. Status Quo
Currently, there is no policy onnoexcept
.
Between 2011 and 2020, noexcept
was applied to the standard library in line with the narrow noexcept
rule [MeredithN3279] [Josuttis0884].
In 2020, the throws nothing rule was proposed [Bergé1656].
The designs that LEWG has forwarded to LWG between 2020 and the present day have been following the narrow noexcept
rule.
There are 78 occurrences of "Throws: Nothing." in the standard [Cpp2023-12], but this number is misleading as many of the usages are in blanket wording that gets applied to multiple classes.
The Microsoft Visual Studio 2022 Preview implementation (version 14.38.32919) annotates each
they strengthen with a
comment.
There are 633 hits of the "noexcept /* strengthened */" string in the headers for this implementation.
All of these are either definitions of functions, or macros that make stamping out
overload definitions easier.
In other words, these hits aren’t counting both forward declarations plus definitions, they are only counting definitions.
This search omits any conditionally
functions.
There are roughly 3,600 instances of the string "noexcept;" in the standard.
A few of these are in examples.
For most
functions, "noexcept;" will show up twice in the standard [Cpp2023-12], once in a synopsis and once in the detailed specification.
This means there are approximately 1,800 unconditionally
functions in the standard.
This search omits any conditionally
functions.
6. Why should we establish any noexcept
policy?
6.1. Core-language Contracts
Interactions betweennoexcept
and the upcoming core-language contracts facilities is likely to be significant.
It would be unfortunate if decisions in the library prevented the core-language contracts work from reaching its full potential.
[Meredith2837] argues that the interaction may be significant and unpredictable enough that it would be better to proceed with no policy in the short term, rather than settle on a policy that will immediately get in the way of contracts.
If we don’t set a policy, then we leave it to paper authors and LEWG to determine the proper
specifier on a per-function basis.
This is likely to cause a great deal of inconsistency in the interim.
6.2. Consistency over preference
There are some design areas where the difference in two choices is of little consequence. LEWG will often spend some time rediscovering the trade-offs and existing practice around the differences, and then make a somewhat arbitrary decision. In these cases, LEWG tends to bias towards "whatever the bulk of the standard is currently doing".
LEWG could become more self consistent by documenting one way, and then requiring authors to either follow the policy, or provide rationale for why the policy should not be applied in this case.
The explicit-ness of multi-argument constructors is one such area [Voutilainen2711].
6.3. Changes in direction
Sometimes, LEWG follows one design pattern in the standard, then discovers or invents a new pattern. The new pattern is inconsistent with the old pattern. Authors then wonder whether to follow the new pattern or old pattern.
This is a reasonable place for LEWG to document a policy.
Using hidden
s for non-member operators is one example of a change in direction.
Whether to make all new "free functions" in the standard library customization point objects (like
) is another.
6.4. Cross group trust
There are a lot of discussions happening concurrently within WG21. Sometimes, one group wants to rely on conventions from another group. These policies help people focus on the groups they are needed, rather than spend a large portion of their time ensuring their policy is followed (or not) in LEWG.
An example policy here would be ensuring that
means either shallow bitwise const or internally synchronized.
The concurrency study group is likely relying on that convention to be followed, so changing
directions would cause surprise.
7. Why the LEWG noexcept
policy isn’t actually that important
Implementations already strengthen the
status of many STL functions.
MSVC STL, libc++, and libstdc++ all mark
,
, and many other functions as
.
Marking existing functions in the standard
will change the results of very few instances of the
operator in practice.
If the committee continues to permit implementations to strengthen the
status of functions, then implementations won’t change.
If the committee chooses to remove this permission, implementations will either ignore the requirement and be non-conforming, or they will accept the code breakage that the backwards compatibility arguments § 8.2 Narrow to wide substitutability / backwards compatibility are trying to avoid.
If we are breaking compatibility in this way once, we may very well break it again in the future.
The main way changing the
policy changes things for standardization and users is if we remove the permission to strengthen
and implementations grudgingly take a one time (but not an N time) compatibility hit.
Note that this paper makes no attempt to change the
strengthening rules in either it’s proposed rules or the presented alternative.
8. Arguments
This section is organized by topic, and not by policy. That means that sections discuss one topic at a time (e.g. testing), and then discuss the affect of the policies on the topic.8.1. Algorithmic optimizations and exception safety
[GregorN2855], [AbrahamsN2983], [MeredithN3248]The original, primary motivation for
was to make it possible for
and similar constructs to take advantage of
optimizations, without breaking existing code (e.g.
’s strong exception guarantee).
This motivation and use case is uncontested in the various papers debating the usage of
, so this paper will not spend further time on it.
On most implementations, disabling exceptions does not change the result of the
operator (The discontinued Intel ICC makes the
operator return true
unconditionally under
).
This means that
is still needed in order to opt-in to algorithmic optimizations, even when exceptions are disabled.
8.2. Narrow to wide substitutability / backwards compatibility
[Doumler2831], [Lakos2949], [Lakos2861]Suppose we have a function
with a precondition of
.
has undefined behavior when the precondition is violated.
Now suppose we have a function
that has the same behavior as
when the precondition is met, but it throws an exception when the precondition is violated.
and
are substitutable for each other, as all in-contract uses behave the same.
Now suppose we have a function
that has the same behavior as
when the precondition is met.
and
aren’t substitutable with each other, due to the change in behavior of the
operator.
returns true
, and
returns false
.
This change in behavior constrains implementations and the C++ committee.
In theory, avoiding
gives WG21 more latitude to make changes in the future.
In practice, this latitude is unneeded, and difficult to use.
The author is unaware of any previous proposal that attempted to make a "Throws: nothing" function released in one standard into a function that could throw something in a later standard. This suggests that this freedom to change isn’t that valuable in practice. We’ve had the freedom, but haven’t needed it or used it.
Using the freedom to make an out-of-contract call throw an exception would be a compatibility break in practice.
MSVC STL, libc++, and libstdc++ all mark
,
, and many other functions as
.
If we were to require
to be
so that it could throw when the container is empty, it would cause a (minor) break for the majority of C++ code bases migrating to that standard due to the change in the
operator’s result.
Perhaps such a break would be small enough to gain consensus in WG21 though.
Major implementations don’t currently use
latitude to implement throwing precondition checks.
The three major implementations (MSVC STL, libstdc++, and libc++) all have checked implementations.
All three use some variation of termination as their mechanism of handling a failed check.
Less prevalent implementations do use such latitude for checked implementations and library testing.
8.3. Codegen changes
8.3.1. "Modern" implementations
Most platforms use the "table-based" implementation strategy for exceptions. The combinations of (x86-64, ARM, Aarch64) X (Windows, Linux, iOS, OSX) all use table-based exceptions.With table-based implementations,
changes the generated assembly.
There are sometimes extra stack manipulations, and minor bookkeeping changes, but no extra branches.
Those minor changes affect the performance in a small and measurable way, but sometimes that difference is positive (for
), and sometimes negative (against
).
The magnitude of the difference is under six cycles for the programs and compilers tested [Craig1886].
Full application testing might show some instruction cache effects, but it is even more likely that any differences would be lost in the noise and immeasurable.
8.3.2. Other implementations
[Doumler2831]Not all implementations use the table-based implementation strategy. These implementations have more substantial bookkeeping relative to table-based implementations.
The most well known of these implementations is the Microsoft Windows 32-bit x86 implementation.
Each new cleanup context requires manipulating a linked list of cleanup actions, even on the happy path.
This results in substantial extra code, and more than a 20% execution performance penalty with micro-benchmarks relative to exceptions being disabled [Craig1886].
With heavy use of
, the overhead can be removed.
The default and recommended exception project settings of the Microsoft Visual Studio compilers is
, which (non-conformingly) makes
functions
by default as a way to reduce exception overhead.
While the days of peak Microsoft Windows for 32-bit x86 are far behind us, new compilers targeting the platform are still regularly released and downloaded. Microsoft Visual Studio 2022 (the latest version at time of writing) still ships compilers targeting 32-bit Windows. Clang 17.0.5 (released on Nov 14, 2023) still targets 32-bit Windows. Download statistics for Clang binaries are available through the GitHub API.
// Clang 17.0.5 download statistices, gathered on Feb 2, 2024 // https://api.github.com/repos/llvm/llvm-project/releases LLVM -17.0.5 - win32 . exe : 1422 downloads LLVM -17.0.5 - win64 . exe : 22370 downloads LLVM -17.0.5 - woa64 . exe : 304 downloads clang + llvm -17.0.5 - aarch64 - linux - gnu . tar . xz : 532 downloads clang + llvm -17.0.5 - arm64 - apple - darwin22 .0 . tar . xz : 585 downloads clang + llvm -17.0.5 - x86_64 …gnu - ubuntu -22.04 . tar . xz : 1803 downloads clang + llvm -17.0.5 - powerpc64 - ibm - aix -7.2 . tar . xz : 69 downloads
Readers should not try to make too many inferences about the relative popularity of platforms based on these numbers, as developers can get compiler releases from multiple sources that wouldn’t register on the GitHub download page. Regardless, the Microsoft Windows 32-bit platform still gets used, even with the most recent compilers, so it should not be brushed off as irrelevant.
James Renwick has an alternative implementation of exceptions [Renwick2019] with the intent of improving determinism in embedded systems.
This implementation also needs to manipulate bookkeeping information in each new cleanup context.
The bookkeeping information is passed along to potentially throwing functions through a hidden parameter.
can remove the cost of the hidden parameter, but can introduce a new cleanup context.
On GPUs, there is a large cost to having additional control flow edges.
Using
can remove control flow edges.
However, to the author’s knowledge, no GPU currently supports C++ exceptions.
8.3.3. Bloat
[Bergé1656], [Doumler2831], [Lakos2861]The "tables" backing table-based exception handling, and the code implementing non-table-based bookkeeping all consume space.
Adding
can reduce code bloat, as this can reduce the amount of information that needs to go into tables or bookkeeping.
Adding
can also cause bloat if the
function calls potentially throwing functions, as a formerly "pass-through" function may need to introduce table entries or bookkeeping to ensure that
gets called.
In Compiler Explorer, bloat is best examined by disabling debug information (
for Clang and GCC), enabling optimizations (
), and leaving directives in the output (uncheck the box in "Filter...").
As a (very rough) estimate of bloat, we can use lines of assembly output as a proxy.
Some of the lines don’t end up as bytes in the resulting binary, but many do show up, often as variable length integers.
Measuring the results on actual binaries is substantially more difficult, due to section alignment quantizing and obfuscating size differences. [Craig1640] presents size benchmarks comparing exceptions to abort, and some of these measurements can be used as proxy byte-based statistics for
bloat.
This code makes it possible to see what happens to the quantity of assembly when switching whether
is
or not, and whether
is
or not. All measurements are in the very rough lines of assembly measurement.
Compiler | calls
| calls
| calls
| calls
|
---|---|---|---|---|
x86 msvc v19.38 | 76 | 25 | 70 | 25 |
x64 msvc v19.38 | 44 | 26 | 37 | 26 |
x86-64 gcc 13.2 | 85 | 20 | 32 | 20 |
x86-64 clang 17.0.1 | 68 | 22 | 76 | 22 |
ARM64 gcc 13.2.0 | 69 | 26 | 46 | 26 |
armv8-a clang 17.0.1 | 74 | 30 | 88 | 30 |
For many platforms and applications, this bloat is inconsequential.
For many platforms and applications that are size constrained, exceptions are already disabled.
When exceptions are disabled, the "
calls
" code and the exception disabled code take the same number of lines of assembly.
8.4. Library Testing
[MeredithN3248], [Doumler2831]Implementations often constrain some undefined behavior for out-of-contract function invocations, particularly in debug and checked modes. "Negative testing" validates that the assertion / contract checks are authored correctly, and that the resulting constrained undefined behavior does the expected thing. The contract checks are code, and code needs testing.
Negative tests can be authored for codebases using the narrow
rule or the throws nothing
rule, but it is substantially easier to do so for narrow
rule codebases.
8.4.1. Core-language contracts
Usage of the proposed core-language contracts facilities will need to be tested.
will present testing challenges when violations of core-language contracts are configured to throw an exception.
Death tests are an option, but they have a large set of challenges on their own.
8.4.1.1. Put the contract check on the outside of the noexcept
One of the design decisions that core-language contracts needs to make is to decide exactly where the checks will be run.
If precondition and post-condition checks happen within the callee (i.e. inside the
), then failed preconditions and post-conditions checks set to throw will cause the program to terminate.
If those checks happen in the caller of the function (i.e. outside the
), then at least one layer of throwing can still work in testing.
Deeply nested checks can still cause a terminate if they end up propagating through a different
.
If precondition and post-condition checks in the caller of a
function, then that would resolve most of the core-language contracts negative testing issues.
Putting preconditions and post-conditions outside the
doesn’t help with core-language contract asserts.
[Voutilainen2780] discusses this point, as well as discussing other benefits involving calling external libraries. It also discusses the challenges of this approach with regards to indirect calls and multiply evaluated preconditions.
Caller-side precondition checking is likely to be less efficient in terms of code size compared to callee-side precondition checking.
8.4.1.2. Retrieve precondition and post-condition results via reflection
Suppose we have the following function + contract:int f ( int x ) noexcept pre ( x >= 0 ) post ( r : r % 2 == 0 );
Now imagine we had reflection facilities that let us do something like the following:
// // evaluate contracts of f(x) without calling f(x) EXPECT_TRUE ( $evaluate_pre ( f , 20 ) ); EXPECT_FALSE ( $evaluate_pre ( f , -1 ) ); EXPECT_TRUE ( $evaluate_post ( f , 2 , 20 ) ); // x = 20, returns 2 EXPECT_FALSE ( $evaluate_post ( f , 1 , 0 ) ); // x = 0, returns 1
This would allow directly testing contracts without violating those contracts. The tests would be very fast (no throws or terminates involved), and low maintenance.
To the author’s knowledge, no such facility is currently proposed. This reflection utility has applications beyond testing, such as automatically generating "wide" wrapper functions from "narrow" functions. This strawman proposal doesn’t address core-language asserts.
8.4.1.3. Allow contract build modes to change the behavior of noexcept
Perhaps we consider noexcept
the first core-language contract, with a default violation handler of std :: terminate
.
The Eval_and_throw
mode could change the behavior of noexcept
to observe exceptions instead of terminate
.
This may even be something that should be extended to a general sad-path post-condition checking facility.
It would be nice if we could express that, on failure,
’s single element insert should leave the container with the same
, and the same
pointer.
Making such sad-path post-condition checks throw on failure would also lead to early termination.
Allowing contract build modes to change the behavior of
could be a concise way to unify
and core-language contracts, but it could come at a substantial compatibility cost for those using core-language contracts.
If their code was relying on
terminating, then enabling
will break their program.
This approach may be able to be combined with § 8.4.7.2 Exploiting precondition undefined behavior to ignore termination to avoid major compatibility breaks.
8.4.2. Extract contract predicates to a function
A user could emulate the precondition reflection by following a good naming convention.bool precondition_f ( int x ) noexcept ; bool postcondition_f ( int retval , int x ) noexcept ; int f ( int x ) noexcept { assert ( precondition_f ( x )); int retval = 0 ; /*...*/ assert ( postcondition_f ( retval , x )); return retval ; } EXPECT_TRUE ( precondition_f ( 20 ) ); EXPECT_FALSE ( precondition_f ( -1 ) ); EXPECT_TRUE ( postcondition_f ( 2 , 20 ) ); EXPECT_FALSE ( postcondition_f ( 1 , 0 ) );
This is an approach that is available for today’s precondition and post-condition checking facilities. A sophisticated user could write static analysis checks to enforce such a convention. The author is unaware of any libraries or guidelines that do this though, which suggests that users either didn’t think of it, or they don’t see this approach as a good return on investment.
8.4.3. The widely deployed implementations don’t diagnose contract violations with exceptions
[Bergé1656], [Doumler2831]In 2023, the three most widely deployed standard libraries are libc++ (shipping with Clang), libstdc++ (shipping with GCC), and Microsoft’s Visual Studio standard library (shipping with Microsoft Visual Studio). None of these implementations use exceptions to diagnose contract violations.
8.4.4. noexcept
macro
[MeredithN3248], [Doumler2831]
Libraries can define a macro like the following:
#if TEST_ASSERTIONS #define MY_NOEXCEPT #else #define MY_NOEXCEPT noexcept #endif
This approach works, but it requires a large number of annotations. Switching between the modes is detectable and increases the number of differences between the test environment and production environment. Each difference between test and production decreases the value of the test.
8.4.5. setjmp/longjmp
[MeredithN3248], [Doumler2831]
and
can be used to bypass
without triggering termination.
It is very difficult to use
and
without triggering undefined behavior though.
That makes
and
generally not viable.
8.4.6. Child threads, fibers, and signals
[Doumler2831]There are other creative ways to write contract handlers that permit testing while avoiding having an exception run into a
. [Doumler2831] describes some approaches for using (and leaking) threads that halt on failure rather than return.
There’s also a description for how to use fibers / stackful coroutines to halt without consuming as many resources as the child thread approach.
Finally there’s a signal based approach.
All of these approaches have substantial downsides.
None of these approaches are in wide use to the best of the author’s knowledge.
8.4.7. Potentially mitigating compiler features
There is the potential to address some of the testability concerns of
with compiler extensions.
These ideas aren’t new. Some of them have been floating around for more than 10 years.
The fact that they haven’t been implemented says something. That something may be "Upstreaming compiler changes is an expensive prospect, and outside the capabilities of most organizations." It may be "I’ll do negative testing in a way that doesn’t require a compiler feature." Or it may be "Negative testing isn’t sufficiently valuable to jump through these hoops." So I’m not entirely sure what the lack of implementations says, but it certainly says something.
8.4.7.1. Compiler flag that makes noexcept
unchecked
The noexcept
specifier has two direct effects on the semantics of a program:
-
exceptions leaving a
function triggernoexcept
, andstd :: terminate -
uses of the
operator on expressions with thenoexcept
specifier change.noexcept
In theory, a compiler could add a non-conforming extensions that removes the
aspects, while keeping the
operator aspects.
This would make it easier to test contracts, as then any exceptions could still escape
functions.
In some ways, this is the inverse of [Halpern2946]. [Halpern2946] proposes a new attribute that (conditionally) keeps the
semantics, but doesn’t modify the
operator’s behavior.
8.4.7.2. Exploiting precondition undefined behavior to ignore termination
If a function does not meet its preconditions, then it is not required to meet its post-conditions. A post-condition ofnoexcept
functions is terminating when there’s an exception.
An implementation could, conformingly, bypass the termination aspect of noexcept
if the program has encountered undefined behavior.
In practice, this may be done by having a "magic" exception type (e.g. nonstd :: undefined_behavior_exception
) that bypasses noexcept
.
Users would then be required to only use that exception when undefined behavior has been encountered.
8.4.8. Death tests
[Bergé1656], [Doumler2831]A commonly recommended approach for negative testing is to use "Death tests". With death testing, the code under test is run in a different process. When the code under test executes a failing contract check, the code under test is expected to exit abnormally. The exit value of the child process is checked in the parent process to ensure that the contract check was triggered appropriately. Multiple standard libraries use death tests for their negative testing.
The largest complaint regarding death testing is the performance penalty. Launching a process is orders of magnitude more expensive than throwing an exception, and this can inflate test times from seconds to minutes when large numbers of negative tests are present. The performance penalty is particularly large on Microsoft Windows.
Traditionally, death tests can communicate a very limited amount of information from the child process to the parent process, typically one integer or less. This makes it difficult to know whether the child process’s abnormal termination was from the intended contract check failure, or some other reason. In theory, the failing checks could communicate through some other out-of-band channel (a file on disk, standard out / error, shared memory), but this isn’t currently common practice.
Launching child processes has other hazards as well.
Using the POSIX
call while code under test is running in another thread introduces substantial testing risks and uncertainty.
If the code under test is not
safe, then death tests can’t be launched with
while the code under test is running.
This can add yet-another performance penalty, as it forces the test to re-run potentially expensive setup code.
The test framework and code under test also have to take care not to run
unsafe code, which can be challenging when global constructors are involved.
A mitigation for this is to have a test launcher that is entirely distinct from the code under test.
and
calls can also be used, but with their own draw backs.
Death tests have portability issues. C++ can run in a variety of environments, and death tests are mostly suited to desktop and server environments. Implementing death tests in browsers and embedded / microcontroller environments would be very challenging, as they typically don’t support multiple processes. In addition, most testing frameworks don’t support death tests, with GoogleTest being the main test framework with that support. GoogleTest mixes the code under test and the test infrastructure in the same process(es).
8.5. Using exceptions to diagnose precondition violations in production
8.5.1. Security dangers of throwing exceptions while in an unknown state
In security sensitive contexts, running out of contract is very dangerous. Each instruction executed while out of contract is another opportunity for an attacker to do as they please with the system. Programs should run as little code as possible after detecting contract violations. This is typically done by exiting the program as quickly as possible.
Throwing an exception runs a lot of instructions.
Many of those instructions are indirect code branches, which are particularly dangerous.
The Microsoft Visual Studio 32-bit x86 implementation has a build flag (
) to harden their exception handling implementation against attacks attempting to leverage those indirect code branches, but the number of instructions run is still very high.
8.5.2. Temporary continuation
[Lakos2861]Following the narrow
policy is more permissive in terms of allowing temporary continuation than the throws nothing policy.
Some users of C++ desire temporary continuation after contract violations, and the throws nothing policy prevents temporary continuation in many places.
8.5.3. Likely to cause more UB or hit a destructor noexcept
Writing exception safe code often requires using operations that aren’t allowed to throw.
A common pattern is to "do all the work off to the side, then commit using nonthrowing operations only" [Sutter82].
If one of the non-throwing operations ends up throwing due to a contract violation, then additional library UB has been added to the program, beyond the initial contract violation.
Throwing from a non-throwing operation can also cause termination in destructors, which are
by default.
So if a contract is violated in a throws nothing function called from a destructor, then termination is the behavior, even if continuation is the desire.
Both of these points illustrate the difficulty and danger in requiring a program to never terminate, even when aided by throwing contract handlers.
8.5.4. Postconditions only hold when preconditions hold
[Krzemieński2858]A general programming principle is that postconditions aren’t required to hold when preconditions don’t hold.
This is often paraphrased as garbage in, garbage out.
If
is a postcondition, then the termination aspect is not required to hold if the precondition doesn’t hold.
Here is an example program where the violation of a library precondition leads to language undefined behavior, with an end result of
being bypassed by an exception.
8.5.5. Precondition UB and noexcept
contradict?
[Doumler2831]
[Doumler2831] argues that
and precondition undefined behavior contradict, as any behavior should be permitted.
prevents the implementer of the function from manifesting some of those behaviors.
For user code, the extent of their undefined behavior is indeed constrained by
.
Standard library implementations are under no such restriction. § 8.4.7.2 Exploiting precondition undefined behavior to ignore termination describes one way that an implementation could choose to throw an exception past
, if they deemed it worth the implementation effort.
8.6. Termination risk
[Doumler2831], [Lakos2861], [Lakos2949]Using
can insert code paths that invoke
.
The terminating code paths are often unreachable code removed during translation, but this isn’t always the case.
There are applications that prefer library UB over
when faced with a failing contract.
Those libraries may use exceptions to indicate failed contracts.
For safety critical systems (note the use of the term "systems", not applications), undefined behavior is generally considered worse than termination, even if the undefined behavior is "only" library UB. In these systems, an individual application may terminate, but the system as a whole has ways to recover from a terminated application, or to put the system into a safe state.
8.7. Philosophy
8.7.1. noexcept
-accuracy
[Bergé1656], [Doumler2831], [Halpern2946]
There is a desire to document the exception behavior of a function in the signature of that function. This documentation helps compilers, static analyzers, and people reading the code to understand what the function does.
[Krzemieński2858] makes the distinction between functions that can’t throw and functions that can’t fail.
C++'s
can’t throw (POSIX’s can as part of thread cancellation).
can fail though, as part of
is flushing buffer contents to storage, and that storage may not be available at time of close (imagine a USB drive being removed).
This separation between can’t throw and can’t fail makes it more difficult to determine what constitutes
-accuracy.
8.7.2. Doesn’t matter since implementations can already add noexcept
[Doumler2831], [Bergé1656]
If the narrow
policy is in place, implementations can strengthen the
annotations on their functions.
Major implementations have done so.
Strengthening
in the standard would end up adding very few
annotations in the major implementations.
8.7.3. Standard library policies vs. C++ library policies
[Lakos2861], [Doumler2831]Many libraries attempt to emulate the design policies of the standard library. The standard library should try to set a good example for third-party and end-user libraries, as others will use the standard library as an example whether the committee thinks they should or not.
The standard library needs to accommodate all domains, serve millions of programmers, and billions of users.
Most other libraries aren’t as widely used.
In some libraries, the customers are known well enough that a compatibility hit from changing
can be absorbed.
The domain and use cases are known well enough that termination on precondition violation can be deemed suitable.
For some libraries, improving some performance aspect (like binary size) by half a percent is worth increasing the cost of testing by a factor of 100. The more heavily used a library, the more likely it is that such a trade-off is worthwhile. Few libraries are used more heavily than the STL.
8.7.4. Standard library implementation strategies need not be encoded in the C++ standard
[Lakos2861]For some standard library implementations, the correct technical and business choice is to terminate on precondition failure via a
decoration.
That is a valid implementation strategy.
However, that doesn’t mean that this implementation should be put in the standard, where it constrains other implementations that don’t want to make that choice.
8.7.5. Cleanup operations often have preconditions, and need to not throw
[Krzemieński2858]Cleanup operations, like
,
, and
all have preconditions, and all need to be made to not throw.
If the preconditions on these functions were checked and made to throw, they would cause the
on destructors to terminate.
If the destructors were marked
, you would still run the risk of termination from when unwinding triggered a precondition fault, causing two exceptions to be active at the same time.
8.7.6. Narrow noexcept
ignores implicit precondition of indeterminate values and invalid objects
[Lakos2949]
The narrow
policy is defined in terms of wide and narrow contracts, but even the wide contracts have their limits.
Take
as an example.
is safe to call on any valid
, even the empty string.
However, if the bytes composing the
are corrupted in some way, then
is likely to dereference an invalid pointer.
This means that
(and most functions) have a precondition of basic object validity.
If wide contracts are expanded to encompass functions that work with invalid objects, then very little has a wide contract.
8.8. Can’t write narrow noexcept
on top of throws nothing
[Meredith2837]
If part of a system is written with a throws nothing policy, then the system as a whole loses the narrow
property.
Systems built with libc++, libstdc++, and the Microsoft Visual Studio standard library generally do not have the narrow
property.
Other libraries can get many of the benefits of narrow
, even if the system as a whole doesn’t have the narrow
property.
Negative testing can be performed on narrow
libraries, as exceptions usually don’t need to travel far.
Preconditions can be checked in production for all the code between entry points and the first non-narrow
code.
Non-narrow
libraries that interact heavily with callbacks can get in the way of both precondition checking and negative testing.
9. Suggested polls
9.1. Either policy is better than no policy
Poll: Having one of the two proposed
policies in place is more important than having my preferred policy, so a head-to-head poll between the two presented alternatives is an acceptable way to select the policy.
noexcept 5-way poll (SF/WF/N/WA/SA)
If the "any policy" poll gains consensus, then we do a head-to-head poll on the policies.
If the "any policy" poll does not gain consensus, then we do two 5 way polls. Whichever poll has consensus in general, and has more consensus than the other, wins. It is possible that neither has consensus. If LEWG is a little crazy, then both may have consensus.
If we do these as electronic polls (and we should), then all four polls should run.
9.2. Head-to-head policy poll
Poll: Which policy do we adopt?
3-way poll (narrow
/"Lakos rule")/Neutral/"Throws nothing rule"
noexcept
9.3. Adopt the narrow noexcept
/"Lakos rule" noexcept
policy
Poll: Add the narrow
/"Lakos rule"
noexcept policy to SD-9
noexcept 5-way poll (SF/WF/N/WA/SA)
9.4. Adopt the "Throws nothing rule" noexcept
policy
Poll: Add the "Throws nothing rule"
policy to SD-9
noexcept 5-way poll (SF/WF/N/WA/SA)