13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Limitations of generics <strong>in</strong> <strong>C#</strong> and other languages109reference types. That lack of shar<strong>in</strong>g does have its benefits, though—it allows typespecificoptimizations, such as <strong>in</strong>l<strong>in</strong><strong>in</strong>g method calls for some type parameters but notothers, from the same template. It also means that overload resolution can be performedseparately for each set of type parameters, rather than just once based solelyon the limited knowledge the <strong>C#</strong> compiler has due to any constra<strong>in</strong>ts present.Don’t forget that with “normal” C++ there’s only one compilation <strong>in</strong>volved, ratherthan the “compile to IL” then “JIT compile to native code” model of .NET. A programus<strong>in</strong>g a standard template <strong>in</strong> ten different ways will <strong>in</strong>clude the code ten times <strong>in</strong> a C++program. A similar program <strong>in</strong> <strong>C#</strong> us<strong>in</strong>g a generic type from the framework <strong>in</strong> ten differentways won’t <strong>in</strong>clude the code for the generic type at all—it will refer to it, and theJIT will compile as many different versions as required (as described <strong>in</strong> section 3.4.2) atexecution time.One significant feature that C++ templates have over <strong>C#</strong> generics is that the templatearguments don’t have to be type names. Variable names, function names, and constantexpressions can be used as well. A common example of this is a buffer type that has thesize of the buffer as one of the template arguments—so a buffer will alwaysbe a buffer of 20 <strong>in</strong>tegers, and a buffer will always be a buffer of 35 doubles.This ability is crucial to template metaprogramm<strong>in</strong>g 15 —an 15 advanced C++ technique thevery idea of which scares me, but that can be very powerful <strong>in</strong> the hands of experts.C++ templates are more flexible <strong>in</strong> other ways, too. They don’t suffer from theproblem described <strong>in</strong> 3.6.2, and there are a few other restrictions that don’t exist <strong>in</strong>C++: you can derive a class from one of its type parameters, and you can specialize atemplate for a particular set of type arguments. The latter ability allows the templateauthor to write general code to be used when there’s no more knowledge availablebut specific (often highly optimized) code for particular types.The same variance issues of .NET generics exist <strong>in</strong> C++ templates as well—anexample given by Bjarne Stroustrup 16 is that there are no implicit conversionsbetween Vector and Vector with similar reason<strong>in</strong>g—<strong>in</strong> this case,it might allow you to put a square peg <strong>in</strong> a round hole.For further details of C++ templates, I recommend Stroustrup’s The C++Programm<strong>in</strong>g Language (Addison-Wesley, 1991). It’s not always the easiest book tofollow, but the templates chapter is fairly clear (once you get your m<strong>in</strong>d around C++term<strong>in</strong>ology and syntax). For more comparisons with .NET generics, look at the blogpost by the Visual C++ team on this topic: http://blogs.msdn.com/branbray/archive/2003/11/19/51023.aspx.The other obvious language to compare with <strong>C#</strong> <strong>in</strong> terms of generics is Java, which<strong>in</strong>troduced the feature <strong>in</strong>to the ma<strong>in</strong>stream language for the 1.5 release, 17 severalyears after other projects had compilers for their Java-like languages.15 http://en.wikipedia.org/wiki/Template_metaprogramm<strong>in</strong>g16 The <strong>in</strong>ventor of C++.17 Or 5.0, depend<strong>in</strong>g on which number<strong>in</strong>g system you use. Don’t get me started.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!