Are Generics in .NET like Templates in C++?

Published 28 July 04 10:42 AM

No.

While Generic types do have a similar syntax to C++ templates, they are instantiated at runtime as opposed to compile time, and they can be reflected on via meta-data.  Also, in Generics, member access on the type paramater is verified based on the constraints placed on the type parameter; whereas, in templates, member access is verified on the type argument after instantiation.

Brandon Bray (Program Manager for the Visual C++ Compiler front-end at Microsoft) has a blog entry where he draws out the distinctions between Generics and Template.

Starting with VS 2005, C#, Managed C++, and VB will have CLR support for generics.  Read these two MSDN Magazine articles for more details. Introducing Generics in the CLR & More on Generics in the CLR

[Author: SantoshZ]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Jerry Pisk said on July 28, 2004 11:26 AM:
Wrong. C++ templates do not say anything how they're handled by the compiler and runtime. The fact that Microsoft's C++ compiler handles them in the simplest but worst possible way (by creating a separate type for each template variation) does not mean that C++ templates are just that. They're not. Generics are a subset of C++ templates, implemented right.
# James Curran said on July 29, 2004 7:51 AM:
Jerry,
huh?

You're ranting about something that not mentioned at all in SantoshZ's original message.

The C++ Standard requires that each template variation be considered a separate type. They can be implemented any way the vendor choices, but, as far as I know, every vendor create distinct instaniations for each type --- not because it's easier, but because it's far more speed efficent (less memory efficent, but memory is cheap and time is money)
# Jerry Pisk said on July 29, 2004 3:40 PM:
It is mentioned - he's right that .Net generics are different than C++ templates but not for the reasons he mentions.
# gool said on August 2, 2004 12:08 AM:
http://www.sj55.com/pic/pic_1392.htm
Believe you, support you, I believe that you are right! ! ! I will make great efforts to look like your study! ! !
# Brandon Bray [MSFT] said on August 2, 2004 8:50 AM:
Addressing some of the feedback items:

To be clear, when the C++ compiler creates a separate type for every template specialization, that does not necessarily mean every type emits separate code. In fact, you'll find that through a feature called COMDAT folding, most templates share quite a bit of code. (Basically, the myth of code bloat for templates isn't true these days.)

By having separate specializations of code at compile-time, the compiler has the ability to optimize each type individually, which includes inlining. If every instance of a function is inlined, the template code is thrown away by the linker resulting in less code. For these reasons, template code is generally much faster and leaner than an equivalent generic alternative.

So, performance and "code bloat" really isn't a valid thing to compare generics and templates. The semantic differences arising from the difference in compile-time verses run-time are far more important. That is what I covered in my blog entry, so if you haven't read it already, I encourage you to do so.
# Rays of Thoughts said on October 20, 2004 8:41 AM:
Templates and Generics
# RebelGeekz said on December 28, 2004 4:54 AM:
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]
# Rays of Thoughts said on March 29, 2006 2:14 AM:
Generics is a feature that has been added to .NET2.0. I have been recently attending lectures on Generics...
# C Frequently Asked Questions Are Generics in NET like Templates in C | debt solutions said on June 19, 2009 1:37 PM:

PingBack from http://debtsolutionsnow.info/story.php?id=10334

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker