Help: Java Generics contrasted to C++ Templates
Hi,
I am intermediate C++ programmer and have decent knowledge of
templates.
In our firm, abstract (generic) libraries are a must. We use C++
templates. Sometimes people argue for using Java because it has
generics which are like C++ templates.
The little I know about both tell me that this is not true. I have
listed below a few of the differences that I think are very bad. Anyone
have a list of things that are missing from java generics in comparison
to templates?
There must be someone more better than me who has such list, but I
can't find it.
Here is my simple list. 1 and 3 are very wrong! They say you can do
that some other way, but it doesn't matter. How can you excuse not
being able to new a type or delcare a type?
If anyone has better list that would be great.
import java.io.*;
import java.lang.*;
/**
* Question to newsgroup: can we new an instance of a template
parameter? (see line 17)
*/
public class Test_q<T>
{
public static void main(String []args)
{
Integer i = new Integer(7);
Test_q r = new Test_q();
r.f(i);
Integer i2 = r.allocate(i);
}
public <E> void f(E item)
{
// (1) cannot new an instance of a template parameter type
E item2 = new E();
// (2) cannot new an array of a template parameter type
E []arr = new E[30];
}
// (3) cannot declare a static variable of a template parameter type
public static T el;
// (4) cannot return an instance of template parameter type
public T allocate(T something)
{
return new T();
}
}
ps: I don't want a religion war; I just want to have a good list so my
firm can have better discussion about this. I don't think any of us has
enough experience and we would like help.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]