Mission Impossible / How to create datatypes which cannot contain invalid state

adrianimboden
23.6K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

The problem

This example represents a quality checker which may fail to check the quality. The failure is no exceptional case, because it is a normal control flow. Because of that, no exceptions are used here.

The invariant of the quality result is, that always exactly one std::optional must be non-empty.

Example

We want to avoid a data structure which has such an invariant, because it is hard to enforce. The calling code has to be written in a defensive way because the invariant is not statically guaranteed.

Try to refactor the example program using std::variant, so that the class ProductProducer never contains unused members.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content