Prev | Current Page 221 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"


These are by no means the only limitations of C# generics, but I believe they??™re the
ones that you??™re most likely to run up against, either in your daily work, in community
conversations, or when idly considering the feature as a whole. In our next two sections
we??™ll see how some aspects of these aren??™t issues in the two languages whose features
are most commonly compared with C#??™s generics: C++ (with templates) and Java
(with generics as of Java 5). We??™ll tackle C++ first.
3.6.4 Comparison with C++ templates
C++ templates are a bit like macros taken to an extreme level. They??™re incredibly powerful,
but have costs associated with them both in terms of code bloat and ease of
understanding.
When a template is used in C++, the code is compiled for that particular set of template
arguments, as if the template arguments were in the source code. This means that
there??™s not as much need for constraints, as the compiler will check whether you??™re
allowed to do everything you want to with the type anyway while it??™s compiling the code
for this particular set of template arguments. The C++ standards committee has recognized
that constraints are still useful, though, and they will be present in C++0x (the
next version of C++) under the name of concepts.
The C++ compiler is smart enough to compile the code only once for any given set
of template arguments, but it isn??™t able to share code in the way that the CLR does with
109 Limitations of generics in C# and other languages
reference types.


Pages:
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233