File: nothrow.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (37 lines) | stat: -rw-r--r-- 1,827 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Exception safety can only be realized if some functions and operations are
guaranteed em(not) to throw exceptions. This is called the
 emi(nothrow guarantee). An example of a function that must offer the nothrow
guarantee is the tt(swap) function. Consider once again the canonical
overloaded assignment operator:
        verb(    Class &operator=(Class const &other)
    {
        Class tmp(other);
        swap(tmp);
        return *this;
    })

If tt(swap) were allowed to throw exceptions then it would most likely
leave the current object in a partially swapped state. As a result the current
object's state would most likely have been changed. As tt(tmp) has been
destroyed by the time a catch handler receives the thrown exception it becomes
very difficult (as in: impossible) to retrieve the object's original
state. Losing the strong guarantee as a consequence.

    The ti(swap) function must therefore offer the nothrow guarantee. It must
have been designed as if using the following prototype (see also section
ref(NOEXCEPT)): 
        verb(    void Class::swap(Class &other) noexcept;)

Likewise, tt(operator delete) and tt(operator delete[]) offer the nothrow
guarantee, and according to the bf(C++) standard destructors may themselves
not throw exceptions (if they do their behavior is formally undefined, see
also section ref(CONSEXCEPTIONS) below).

    Since the bf(C) programming language does not define the exception concept
functions from the standard bf(C) library offer the nothrow guarantee
by implication. This allowed us to define the generic tt(swap) function in
section ref(CopyDestroy) using ti(memcpy).

    Operations on primitive types offer the nothrow guarantee. Pointers may be
reassigned, references may be returned etc. etc. without having to worry about
exceptions that might be thrown.