One of the most awaited features of Microsoft .NET 2.0 is generics. Generics promise to increase type safety, improve performance, reduce code duplication and eliminate unnessecary casts. The most obvious application of generics in the framework class library are the generic collections in the new System.Collections.Generic namespace. Much has been written about those, but they are not the topic of this article.
Permalink for comment
To read all comments associated with this story, please click here.
The dynamic vs. static discussion is beside the point.
From a programmer's perspective, generic classes in C# are instantiated and type-checked at compile-time, as are C++'s template classes. (How compiler and run-time system implement them is another matter.)
Yes, reflection in C# allows generic classes to be instantiated at runtime, but that's more complicated, more error-prone and slower, so it should only be done where the added flexibility is absolutely necessary.
Another difference between C# generics and C++ templates is much more significant. C# supports type parameter constraints and type-checks generic classes much like normal classes, whereas template classes are type-checked whenever they're instantiated.
C++'s concept is more flexible but has two significant drawbacks. First, type correctness of a template can not be proven by the compiler and has to be ensured through test instantiations, which of course can leave untested cases. Secondly, violations of the implicit constraints on template arguments lead to unhelpful error messages somewhere inside the template.
Recent C++ compilers can do some type-checking on templates, but that can only ever be partial because there are no explicit type constraints to check against.
The "Boost Concept Checking Library" for C++ uses some ingenious macro hackery to allow explicit template constraints and provide better error messages. But again that's only a partial solution because the template writer isn't required to provide these and the compiler cannot check that the template code actually adheres to the constraints.
The dynamic vs. static discussion is beside the point.
From a programmer's perspective, generic classes in C# are instantiated and type-checked at compile-time, as are C++'s template classes. (How compiler and run-time system implement them is another matter.)
Yes, reflection in C# allows generic classes to be instantiated at runtime, but that's more complicated, more error-prone and slower, so it should only be done where the added flexibility is absolutely necessary.
Another difference between C# generics and C++ templates is much more significant. C# supports type parameter constraints and type-checks generic classes much like normal classes, whereas template classes are type-checked whenever they're instantiated.
C++'s concept is more flexible but has two significant drawbacks. First, type correctness of a template can not be proven by the compiler and has to be ensured through test instantiations, which of course can leave untested cases. Secondly, violations of the implicit constraints on template arguments lead to unhelpful error messages somewhere inside the template.
Recent C++ compilers can do some type-checking on templates, but that can only ever be partial because there are no explicit type constraints to check against.
The "Boost Concept Checking Library" for C++ uses some ingenious macro hackery to allow explicit template constraints and provide better error messages. But again that's only a partial solution because the template writer isn't required to provide these and the compiler cannot check that the template code actually adheres to the constraints.