Exceptional safety: the benefits of nothrow replacement

If a type has a swap function that cannot fail, it can make it easier for other functions to provide a strong security guarantee . This is due to the fact that we can first do all the work of the function, which can go out of order, and then do the work using non-flipping swaps.

However, are there any other benefits of making sure swap never fails?

For example, is there a situation where having a no-fail shift makes it easier to provide another function with a basic guarantee?

+3


source to share


1 answer


Let's say I do this:

class C {
  T a, b;  // invariant: a > b
  void swap(C& other);
};

      



There seems to be no way to implement C :: swap () with a basic guarantee if T :: swap (T &) can throw. I have to add a level of indirection and keep T * instead of T.

0


source







All Articles