Can C++ provide class invariants?
There is a long-standing wish of equipping C++ classes with invariants. The wish was mentioned by, Stroustrup the designer and original implementer of C++. Ironically, it is precisely the design of C++ exceptions that takes away any hope of extending C++ class to include invariants.
In order for the notion of invariant to be of any use, the runtime library needs to throw an exception in case the conditions stated in the invariants of a class are violated. However, C++ can only catch exceptions thrown within the C++ source code, not those coming from external sources such as the operating system or the runtime library.
Thus, the only possible implementation of invariants is that the programmer writes the code for testing the invariants at end of each (public) method of a class. The tail-code can then throw exceptions as needed. In other words, any mechanism for specifying invariants as part of the definition of a class will serve no purpose beyond documentation.
Furthermore, catching a C++ exception can hardly be used for anything beyond terminating the program. So the question is, what purpose is served by adding invariants? After all C++ does not offer any form of resumption.
Z++ notion of class includes invariants, as well as method-constraints.
In order for the notion of invariant to be of any use, the runtime library needs to throw an exception in case the conditions stated in the invariants of a class are violated. However, C++ can only catch exceptions thrown within the C++ source code, not those coming from external sources such as the operating system or the runtime library.
Thus, the only possible implementation of invariants is that the programmer writes the code for testing the invariants at end of each (public) method of a class. The tail-code can then throw exceptions as needed. In other words, any mechanism for specifying invariants as part of the definition of a class will serve no purpose beyond documentation.
Furthermore, catching a C++ exception can hardly be used for anything beyond terminating the program. So the question is, what purpose is served by adding invariants? After all C++ does not offer any form of resumption.
Z++ notion of class includes invariants, as well as method-constraints.
Labels: class invariants, Z++
0 Comments:
Post a Comment
<< Back to Blogger Start Page >>