Utility Functions Constrained
Get an overview of how the utility functions can be constrained.
We'll cover the following
Utility functions are most often free or static functions. They are meant to be used with certain user-defined types.
Usually, utility functions make sense only with a handful of types. If the number of types is limited enough, or maybe if they are tied to a class hierarchy, using these utilities is straightforward.
However, if the available types are broad enough, they are often templatized. In such cases, documentation and (template) parameter names can come to the rescue. It’s not optimal, but it still helps. Regardless, these utility templates often have no documentation and are poorly named.
The best documentation is code. The best way to document behavior is through unit tests and code that expresses its intentions. Code should do that, preferably by compilation errors, or at worst with runtime failures.
What concepts provide
Concepts provide a concise way to tell the reader about the types that are supposed to be used with constrained entities. In this case, concepts help us with the utility functions.
Get hands-on with 1200+ tech skills courses.