1

I've already noticed that C# generics are quite different from C++ templates. I've read this question and answers to it and got a basic understanding of what the difference is.

However I don't get the reason for that design difference. I mean C++ was already quite developed when C# was being designed. What was suboptimal in C++ templates that C# developers did not clone C++ templates but instead rolled in generics that are so massively different?

What can generics do what C++ templates can't? In what are C# generics better than C++ templates? What's the reason for designing C# generics behaving differently from C++ templates?

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979

3 Answers3

4

The reason is that one ARE TEMPLATES the other are not. Sounds redundant? Partially because you ask a question oalong "why is a car different from a plane".

Templates are a very complex mechanism and most of the compelxity is rarely used. I think you don't really know what templates can do - only the 25% of C++ templates people use every day. This is the main problem - they are too comlpex. Hard for the compiler.

So Generics are supposed to capture a different approach for the most common use of templates, which is generic general types (no specialization, for example).

What can generics do what C++ templates can't?

Nothing.

What's the reason for designing C# generics behaving differently from C++ templates?

The fact that C++ generics are hardly mastered by anyone and a terrible thing to implement correctly for the compiler, while mostly going unused.

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • 1
    Could you please provide an example of C++ template functionality that is clearly out of those 25%? – sharptooth Aug 26 '11 at 09:00
  • http://www.gotw.ca/publications/mill17.htm for example discussing one of the problematic nice features. – TomTom Aug 26 '11 at 09:06
  • couldn't resist. You say: "because you ask a question oalong "why is a car different from a plane"." and then go on to answer the question: "What can generics do what C++ templates can't?" with "Nothing.". A plane can fly a car cannot :-). Now getting serious, You were initially right generics and templates are quite different things. The fact that generics remain an "open" type after being compiled and linked to bytecode while templates are static compile time evaluated also means that you can do things with a generic type that are not possible with a template after both are compiled/linked. – ds27680 Aug 26 '11 at 17:01
4

The main reason that it works completely different.

C++ templates are evaluated at compile time.
C# generics are evaluated at run time.

Besides, c++ templates are complicated. When developing C# they decided to reduce the complexity of the templates. Both for the users and for the run-time evaluation implementation.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • 1
    "C# generics are evaluated at runtime", huh? We get static type checking with C# generics. Do you have a link? – George Duckett Aug 26 '11 at 08:58
  • 3
    He is right, though. Generics compile into one open class bytecode wise that ist hen at run time closed to a specfiic type class. Templates are hardcoded. A List in C# exists as List<> temaplte class, in C++ there is no List<> tempalte after the compiler. – TomTom Aug 26 '11 at 09:00
  • 1
    @George Duckett: C# has reflection. This allows you,among other things, to dynamically get types at run time. And you can create a generic class with that type at run-time. The static type checking checks that for any type T in the generic you can only assign a T. So at run-time it will work for any type. If a type missmatch still occurs at run-time (due to abusive tricks usually) you will get an exception. – Yochai Timmer Aug 26 '11 at 09:24
  • @TomTom: Generics are compiled just in time. That is *not* the same as being evaluated at run time, which implies that every time your program does something with a generic class or method the whole process is done anew. @ Yochai: Reflection has nothing to do with it. Yes, with reflection you can, at run time, create generic things. You can also create anything else, all of which are normally considered at compile time. Compile time, of course, always happens just before run time, since C# is just in time compiled. – Joren Aug 30 '11 at 10:47
1

What can generics do what C++ templates can't?

  • Generics have runtime information on instantiated types. This is useful for introspection (reflection) and appliances that use it;
  • Generics on C# 4.0 supports covariance and contravariance on interface and delegate types. So, as a little example, an IENumerable<Derived> can be used when an IEnumerable<Class> is requested, where Derived inherits from Class.
ceztko
  • 14,736
  • 5
  • 58
  • 73