In theory, practice and theory are the same, in practice they are not. So, after having read how brilliant and smart smart-types are, it is now time to have a closer look at the compiler and figure out what C++ can offer.
After my last post, I found that Smart Types are also known as Refined (or refinement) Types. And here is a notable implementation for Scala.
Simple things first, if you need a type with a bunch of possible values, don’t use int
& #define
s, don’t use bool either (please), use enum, or, even better enum class.
Now that we’ve done with the trivialities, let’s proceed to something more challenging – numeric types. Ideally, we want some template code that wraps the numeric type and saves us the boredom of writing all the usual +, -, *, /, ==, !=, <… operators, while letting us define the rules of the existence of the represented type.
Continue reading “As Smart as a Smart Type – Practice (C++)”