3 ms·
My one big beef with generics is that they only work through constraints! With templates, the compiler checks the validity of a template only when applying it t
by pif 10y ago
My one big beef with generics is that they only work through constraints! With templates, the compiler checks the validity of a template only when applying it to a concrete type; a generic, OTOH, needs to be valid as an independent entity. And this means that you are stuck with the limitations of the constraints: you cannot specify that your type has a constructor accepting parameters, you cannot specify static methods, nor operators.
With generics, you can only pass complete types as generic parameters; templates accepts types, templates and integral constants.
You can't have partial specializations with templates.
Typedef. This is the most annoying limitation I found in C#. You cannot type:
typedef vector<MyClass> MyList;
and have it valid in the whole project.
- xenadu02 10y agoThat's not inherent to generics - see Swift, which absolutely allows protocols to require static/class members or initializers. Swift 3 also adds generic type aliases (typedefs). Having worked extensively with both I think Swift learned a lot from C# (and many other languages). As for Concepts... No one seems to be able to agree on them so I'm not holding my breath.