In C++, templated code are compiled multiple times according to the parameters. But I just realized how restrictive C# generics are:
- The interfaces you are using on variables in a generic parameter type must be specified on the parameter.
- There isn't a feature like explicit template specification, to get unrelated types from different types as inputs.
- The implementable interface members are technically not called virtual, but just like virtual. They need lookups anyway, and there is nothing to optimize.
There is a case that seems to require compiling multiple times, that is to define variables of different sizes. But most types in C# are references of fixed size. For the purpose of this question, let's assume variables are always references of fixed size and ignore other cases in a hypothetical language.
In this case, are there benefits of compiling this kind of generics to multiple instances of target code? If there are, where and why? If there are not, is this the exhaustive list of reasons why it doesn't need recompiling like C++? It's also helpful to know how C# is really implemented.
(They are termed monomorphization and type-erasure as two complication strategies. My question is, in this specific case, do they actually make any differences, to make it even meaningful to choose between something in a strategy?)