C++ Strong Duck

Not a Duck. Amazing how similar. Obviously we need Duck Strong Type.
Not a Duck. But, amazingly similar. Obviously we need Duck Strong Type, to avoid mistakes.

I admit. C++ Strong Duck , is a click bait title. Perhaps.

Please first make a slight detour into the direction of “Strong Type by Struct“.

“Just make everything to be a class”. Primordial C++ idea. But, I don’t care if it is. It works. If used but not overused. There is no complex C++ lib behind. Almost nothing.  Just the agreement.

Make every “strong type” as one struct, with a single member named ‘v’. Name of the struct is the strong type name. Type of the v is defined by you.

Let the code speak.

This is my minuscule macro, that works for me. We do not want to mix kilograms and millimeters. So lets make them as strong types.

And now we have some creatures to manage. We use the same ridiculously simple macro to produce much cleaner code.

Ah OK, here is the first duck flying in. Lets take care of it.

Duffy is immutable, but it has gained in weight

There are no methods, here, one can indeed mistake height for weight when giving them values, but everybody knows what to do about that.

The point is we have described a Duck as a strong type. Much (much) better than:

One can say: “Strong Duck” typing indeed :)

https://cppx.godbolt.org/z/rWW5zI

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.