C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help: Java Generics contrasted to C++ Templates

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sat Jul 29, 2006 2:08 am    Post subject: Help: Java Generics contrasted to C++ Templates Reply with quote



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! ]
Back to top
Sean Kelly
Guest





PostPosted: Sat Jul 29, 2006 9:26 pm    Post subject: Re: Help: Java Generics contrasted to C++ Templates Reply with quote



moleskyca1 (AT) yahoo (DOT) com wrote:
Quote:
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?

Read this article: http://www.artima.com/intv/generics.html

In short however, Java generics are essentially a code generation tool
on top of old-style Java containers (that store everything as an
Object)--a cast is implicitly performed for 'get' operations, and some
checking is done for 'set' operations, but the system is fairly easy to
subvert, particularly in large projects.


Sean


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jeremy
Guest





PostPosted: Wed Aug 02, 2006 2:44 am    Post subject: Re: Help: Java Generics contrasted to C++ Templates Reply with quote



Another feature of C++ templates that is not provided by Java
Generics is template specialization. Thus, many common C++
idioms like traits (e.g. iterator_traits) and all the techniques
associated with template metaprogramming, can not be
used in Java.

Cheers,
Jeremy

moleskyca1 (AT) yahoo (DOT) com wrote:
Quote:
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! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.