Prev | Current Page 222 | Next

Jon Skeet

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

That lack of sharing does have its benefits, though??”it allows typespecific
optimizations, such as inlining method calls for some type parameters but not
others, from the same template. It also means that overload resolution can be performed
separately for each set of type parameters, rather than just once based solely
on the limited knowledge the C# compiler has due to any constraints present.
Don??™t forget that with ???normal??? C++ there??™s only one compilation involved, rather
than the ???compile to IL??? then ???JIT compile to native code??? model of .NET. A program
using a standard template in ten different ways will include the code ten times in a C++
program. A similar program in C# using a generic type from the framework in ten different
ways won??™t include the code for the generic type at all??”it will refer to it, and the
JIT will compile as many different versions as required (as described in section 3.4.2) at
execution time.
One significant feature that C++ templates have over C# generics is that the template
arguments don??™t have to be type names. Variable names, function names, and constant
expressions can be used as well. A common example of this is a buffer type that has the
size of the buffer as one of the template arguments??”so a buffer will always
be a buffer of 20 integers, and a buffer will always be a buffer of 35 doubles.


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