Prev | Current Page 223 | Next

Jon Skeet

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


This ability is crucial to template metaprogramming 15??”an15advanced C++ technique the
very idea of which scares me, but that can be very powerful in the hands of experts.
C++ templates are more flexible in other ways, too. They don??™t suffer from the
problem described in 3.6.2, and there are a few other restrictions that don??™t exist in
C++: you can derive a class from one of its type parameters, and you can specialize a
template for a particular set of type arguments. The latter ability allows the template
author to write general code to be used when there??™s no more knowledge available
but specific (often highly optimized) code for particular types.
The same variance issues of .NET generics exist in C++ templates as well??”an
example given by Bjarne Stroustrup16 is that there are no implicit conversions
between Vector and Vector with similar reasoning??”in this case,
it might allow you to put a square peg in a round hole.
For further details of C++ templates, I recommend Stroustrup??™s The C++
Programming Language (Addison-Wesley, 1991). It??™s not always the easiest book to
follow, but the templates chapter is fairly clear (once you get your mind around C++
terminology and syntax). For more comparisons with .NET generics, look at the blog
post by the Visual C++ team on this topic: http://blogs.msdn.com/branbray/
archive/2003/11/19/51023.


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