Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

What sets C++ Templates and .NET Generics apart?

C++ Templates and .NET Generics are not the same. There are many differences between them, but the biggest thing that sets them apart is that C++ Templates are instantiated at compile-time while Generics are instantiated at run-time.

Since C++ Templates are instantiated at compile-time, a copy of the code for each instance is generated, replacing the type placeholders with the actual data type. It is the equivalent of creating separate methods that handle several data types. In other words, declaring List, List, List, and List will cause the compiler to generate code for each declaration. Even though there are two Lists of integers, the compiler will still generate the code for both Lists. This seems repetitive, but pretty harmless on the surface. However, what if you have two assemblies where List in Assembly1 is dependent on List in Assembly2, but List in Assembly2 is seen as a completely different type then List in Assembly1? This presents a problem as it is like attempting to return a long or another data type other than an integer.

On the other hand, since Generics are instantiated at run-time, generic specializations are created on demand and share the underlying code whenever possible. This resolves the problem present in C++ Templates as mentioned above.

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Tuesday, December 20, 2005 1:31 PMby Ryan Olshan
Filed under:

Comments

No Comments

New Comments to this post are disabled