Strong exclusion guarantee VS Basic exclusion guarantee

According to Abrahams, we have 3 types of exceptions:

  • Nothrow
  • Basic exclusion guarantee
  • Strong exception guarantee

Fixed assets ( please correct me if I am mistaken ) that invariants are preserved, for example, that component invariants are preserved and no resources are leaking, where it is Strong that the operation completed successfully or throws an exception, leaving the program state exactly as it was before the operation.

  • What does conservation of invariants mean? what if I have a valid value in one of my variables, then it won’t (take, for example, a pointer), a NULL ?

  • Referring to a strong exception guarantee, does this mean that all my variables will keep the same values ​​before the exception is thrown?

eg:

 int main() { int j = 1; int *p = &j; // do some stuff j = 2; throw 1; } 

Then after I throw, will j hold the value 2 or 1 ?

Hi

+7
source share
2 answers
  • Basic Warranty: Once an exception is thrown, objects are stored in a consistent, usable state. Resources do not leak, and invariants are preserved. The state of the objects may have changed, but it is still usable. For example, date objects whose day value is -1 are no longer used. Invariants say that day is in [1; 31].

  • Strong guarantee: (in addition to 1.) The date object is 2012-12-31. After the operation that attempts to change this value has failed, the value of this object still remains in 2012-12-31. Perhaps some kind of internal state has changed, but the logical state from the client’s view has not changed.

+6
source

In your case there is no guarantee of exclusion. (This is basically case 0). The Wikipedia article you are quoting is understandable: "Rules apply to class implementations." Also, after you have chosen, the variable j goes beyond the scope and no longer exists. You can’t even talk about your address anymore, not to mention the meaning

Typically, class invariants are determined by the author of the class, so it means everything that the class author has in mind. I do not understand your point 1. NULL is a valid value for a pointer.

The second point is good. The definition is not absolute. For example, an operation on a string data item may increase its capacity. You can watch it outside through const& . However, this throughput is usually not considered as part of the string value and, therefore, is not part of the class invariant.

+2
source

All Articles