3 ms·
> I guess I'm just one of the old-fashioned folks who think that just because something was difficult to write, it shouldn't be difficult to read. Code clarity
by jlarocco 2y ago
> I guess I'm just one of the old-fashioned folks who think that just because something was difficult to write, it shouldn't be difficult to read. Code clarity wins over brevity in my book any day.
I treat template metaprogramming in C++ like macro code in Lisp - it's not something I use all the time and it's often more complicated than every day code, but used well it make the rest of the codebase simpler to understand.
The most complicated template I've written recently wraps a vendor's clunky old iterator classes (unrelated to STL) with .Current() and .Next() methods, and lets us use them in C++11's `for (auto thing : VendorIterator)` loops. In the vendor's defense, they started writing the library in the early 90s, before the STL was formally part of C++ - though it would be nice if they provided a template like this themselves.
In any case, the template greatly simplifies using the iterators (5 lines of code goes to 1) in dozens of places, at the cost of a complicated template that rarely needs to change. To me it's a fair tradeoff.