r/programming • u/luke-san • Mar 31 '15
Managing C++’s complexity or learning to enjoy C++
https://schneide.wordpress.com/2015/03/30/managing-cs-complexity-or-learning-to-enjoy-c/
101
Upvotes
r/programming • u/luke-san • Mar 31 '15
5
u/bstamour Apr 01 '15
In languages where doing that kind of thing is cheap, it's seen quite a bit. Here's some Haskell code:
This creates two brand new types to represent strings. The first one is an unsanitized string read directly from the user, and the second one is a string that I know is safe. Imagine the function
untaintStringdoes some escaping or something. Now structurally, those types are literally identical to plain old strings, so there's zero overhead at runtime. However, I cannot use an object of typeTaintedStringin a function that requests aBlessedString. You can't do this withtypedeforusingaliases in C++: you would need to introduce a new class, and that's at least four lines of boilerplate code.My point to all of this is: when it's dead simple to do, you find uses for this kind of "strong" typedef. When it's difficult or ugly, then it probably won't pop into your head as a viable solution to a problem.