Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Differences between C++ Templates and Java/C# Generics and what are the limits? [closed]

I read an interesting Article/Thread/Discussion from here and i got following questions:

  • What are the limitations of Java/C# generics?
  • What is possible with C++ Templates that is impossible with Java/C# generics?

Edit 1 More recommended questions by Eric Lippert

  • What are some patterns that are possible with C# generics but impossible with C++ templates?
  • What's the difference between C#'s true generic types and Java's type erasure generic types?
like image 707
Quonux Avatar asked Apr 07 '13 00:04

Quonux


People also ask

What are templates in Java?

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

Are there templates in Java?

There are no real templates in Java. C++ templates are compile-time entities that are used to generate classes. At runtime, there is no trace of them. In Java, there are parameterized types as part of a mechanism called generics.

Are C++ templates and Java generics the same?

Key differences between generics and C++ templates: Generics are generic until the types are substituted for them at runtime. Templates are specialized at compile time so they are not still parameterized types at runtime.

How Java is different from C and C++?

C++ is derived from C and has the features of both procedural and object-oriented programming languages. C++ was designed for application and System development. Java is built upon a virtual machine which is very secure and highly portable in nature.


2 Answers

First off, you might want to read my 2009 article on this subject.

The primary difference to my mind between C++ templates and C# generics is that C++ templates actually completely recompile the code upon construction of the template. The pros and cons of the C++ approach are many:

  • PRO: You can effectively create constraints like "the type argument T must have an addition operator"; if the code contains a couple of Ts added to each other then the template will not compile if you construct it with a type argument that doesn't permit addition.

  • CON: You can accidentally create undocumented constraints like "the type argument T must have an addition operator".

In C# you have to say what the constraints are which helps the user, but you are limited to only a small set of possible constraints: interfaces, base classes, value vs reference type and default constructor constraints, and that's all.

  • PRO: Semantic analysis can be completely different for two different constructions. If you want that, that's awesome.

  • CON: Semantic analysis can be completely different for two different constructions. If you don't want that, that's a bug waiting to happen.

In C# the semantic analysis is done once no matter how many times the type is constructed, and it is therefore required to work with any type argument that meets the constraints, not just the type arguments that are actually supplied.

  • PRO: You only generate the code for exactly the constructions you need.

  • CON: You generate the code for all the constructions you use.

Templates can cause codegen to get large. In C#, the IL for a generic type is generated once, and then at runtime the jitter does codegen for all the types your program uses. This has a small performance cost, but it is mitigated somewhat by the fact that the jitter actually only generates code once for all reference type arguments. So if you have List<object> and List<string> then the jitted code is only generated once and used for both. List<int> and List<short> by contrast jits the code twice.

  • PRO: when you use a template library, you have the source code right there.

  • CON: to use a template library you have to have the source code.

In C#, generic types are first-class types. If you stick them in a library, you can use that library anywhere without having to ship the source code.

And finally:

  • PRO: Templates permit template metaprogramming.

  • CON: Template metaprogramming is hard to understand for novices.

  • CON: The template system actually does not permit some type topologies that are extremely straightforward in a generic system.

For example, I imagine that it would be difficult to do something like this in C++:

class D<T> 
{
    class S { }
    D<D<T>.S> ds;
}

In C# generics, no problem. At runtime the type is only built once for all reference type arguments.

But in C++ templates, what happens when you have D<int>? The interior type constructs a field of type D<D<int>.S>, so we need to construct that type. But that type constructs a field of type D<D<D<int>.S>.S>... and so on to infinity.

like image 147
Eric Lippert Avatar answered Oct 24 '22 03:10

Eric Lippert


What are the limitations of Java/C# generics?

Java generics are limited because it is not possible to do some tricks like in C++.

To proof the claim here is an C++ example that is impossible to reproduce with templates alone in Java.

Policy based programming is a way to restrict the use of an (templated) class to other (possible) templated classes on inheritence at compile time.

What is the big deal of compile time generics vs runtime generics?

The deal is that the compiler knows everything about the possible runtime behaviour of the class/templates so it can do heavy optimizations that are (currently) impossible with an C#/Java/whatever runtime environment/compiler.

A other good side of this is that the compiler can ensure that the instatiation of a combination of the templates is valid which means that there is no possibility that runtime-errors can happen like in Java/C# when the programmer want to instatiate new objects with an invalid combination.

What is the downside of C++ generics?

The downside is that the templates can get really complicated to read and understand and debug. This is (maybe) one of the reasons why the Java developers didn't want to have such a beast in a language.


What is possible with C++ Generics that is impossible with Java/C# generics?

In C++ is is possible to use other templates as template parameters which is impossible in C#/Java and which allows elegant tricks like template-metaprogramming.

like image 22
Quonux Avatar answered Oct 24 '22 01:10

Quonux