2 ms·
> Sometimes I think templates/generics is all just a hack to get around the fact that I can't add interfaces to types I didn't define myself You can though. Th
by nly 11y ago
> Sometimes I think templates/generics is all just a hack to get around the fact that I can't add interfaces to types I didn't define myself
You can though. They're called free functions. Types should only expose their basis functions[0] as members, keeping their surface small. Once you have that small set of reasonable functions, you can write free functions to do everything else. Sometimes if several of your operand types share a minimal set of basis functions it's useful to make these algorithms templates. That's all.
Scott Meyers was writing about this stuff 20 years ago. Here's a 15 year old example[1]. Here's another, more recent, article[1] by Walter Bright, which compares the approaches of C#, C++ and D.
This is the reason why C++ has the 'madness', as some people see it, of function overloading and Argument Dependent Lookup[3] and, soon, a Unified Call Syntax of its own.
So you want all classes derived from an Object base. Fine. What are you basis functions for Object?
[0] https://en.wikipedia.org/wiki/Basis_function https://en.wikipedia.org/wiki/Basis_function
[1] http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197?pgno=3 http://www.drdobbs.com/cpp/how-non-member-functions-improve-...
[2] http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394 http://www.drdobbs.com/cpp/uniform-function-call-syntax/2327...
[3] http://en.cppreference.com/w/cpp/language/adl http://en.cppreference.com/w/cpp/language/adl