J2SE 5.0 Generics and C++ Templates

S

sarathy

Hi all,
Is there any relation between the J2se 5.0 generics and
C++ templates ??

Regards,
Sarathy
 
E

EJP

Darryl said:
Not really, no, except in concept.

Not really, no, except in *syntax*.

C++ templates are a code-generation mechanism. Java Generics are a
type-constraint system which generate no code at all.
 
D

Darryl L. Pierce

EJP said:
Not really, no, except in *syntax*.

C++ templates are a code-generation mechanism.

Which is implementation, and why I said "in concept".
Java Generics are a type-constraint system which generate no code at
all.

But both work on the same basic concept: providing a mechanism for
writing code in a generic way and allowing a developer to specify the
desired functionality at compile-time.
 
M

Michael Redlich

sarathy said:
Is there any relation between the J2se 5.0 generics and
C++ templates ??

Hi sarathy:

My answer will have a slightly different perspective...

The C++ templates mechanism is considered generic programming, so based
on that alone, I would have to say "yes" to your question, assuming of
course, that the relation between generics in C++ and Java is purely
based on concept.

I totally agree with everyone else that the implementation of generics
in both languages is vastly different. C++ templates have been around
quite some time, and are part of the Standard Template Library (STL)
that includes a comprehensive set of iterators, containers, and
algorithms.

I applaude Sun Microsystems for adding generics to Java, because I
always found the C++ Standard Template Library (STL) to be a powerful
tool for application development.

Hope this helps...

Sincerely,

Mike.
 
C

Chris Smith

Michael Redlich said:
The C++ templates mechanism is considered generic programming, so based
on that alone, I would have to say "yes" to your question, assuming of
course, that the relation between generics in C++ and Java is purely
based on concept.

While I don't completely disagree that there is any connection, I think
this is likely to be misleading. While templates are a mechanism for
generic programming in C++, "generics" in Java are (perhaps confusingly)
not a mechanism for generic programming. Rather, they are a mechanism
for adding type safety to generic programming. In Java, the mechanism
for generic programming is to make use of base classes, including the
universal base class Object. That's been done, of course, since Java
1.0. The type system support is the only thing that's new.

That said, Java's generics are used mainly in places where generic
programming is going on, so they do often appear in the same places as
C++ templates -- most obviously, in container and iterator classes and
supporting algorithms.
I totally agree with everyone else that the implementation of generics
in both languages is vastly different.

I hope I've been able to explain why it's not just the implementation
that's different.
 
E

EJP

Darryl said:
Which is implementation, and why I said "in concept".

I think it's more than just implementation actually. C++ templates are
*conceptually* associated with the idea of 'instantiation' per type.
But both work on the same basic concept: providing a mechanism for
writing code in a generic way and allowing a developer to specify the
desired functionality at compile-time.

Desired functionality generally *is* specified at compile-time by
developers! There's nothing unique to templates/generics about that!

Doesn't leave much. I agree that they both provide mechanisms for
writing code in a type-generic way.
 
M

Mark Space

EJP said:
Darryl L. Pierce wrote:
Doesn't leave much. I agree that they both provide mechanisms for
writing code in a type-generic way.

So: type generic code, and angle brackets. That's it?
 
C

Chris Uppal

EJP said:
I think so, and to be honest I wish Java didn't have the angle brackets.

At least the Java parser understands that << doesn't /always/ mean "right
shift"...

BTW, I think you are over-stating the similarity between Java's generics and
C++'s templates. IMO, they are very different semantically, the overlap is
that they have superficially similar syntax, and that they are sometimes used
for the same things. But the fact that they are used for some of the same
things, doesn't mean they are the same, or even similar. Higher-order
functions and objects are also used for (sometimes) similar things, but that
doesn't mean they are the same.

-- chris
 
J

Jerry Coffin

At least the Java parser understands that << doesn't /always/ mean "right
shift"...

That would be the lexer rather than the parser -- and of course, a right
shift would be '>>' rather than '<<'.

The next version of C++ will also (apparently) change the lexer so '>>'
can be treated as two symbols. Personally, I'm not entirely thrilled
about that though -- there's code that's currently legitimate that could
become ill formed and/or change meaning when this change happens. I'm
fairly sure I've never seen real code like this though, so it's probably
not a big deal.
 
E

EJP

Chris said:
BTW, I think you are over-stating the similarity between Java's generics and
C++'s templates.

Overstating? everybody else thought I was understating! I got it down to
'type-generic code' (not my term): wasn't that far enough?
 
M

Michael Safyan

EJP said:
Overstating?
Yes.
everybody else thought I was understating! I got it down to
'type-generic code' (not my term): wasn't that far enough?

Java Generics do not support specializations nor do they support
parametrization of non-types. These two key features lead C++ templates
to greatly differ from Java generics.
 
E

EJP

Michael said:
Yes.
everybody else thought I was understating! I got it down to



Java Generics do not support specializations nor do they support
parametrization of non-types. These two key features lead C++ templates
to greatly differ from Java generics.

Thank you. We are in complete agreement and that follows directly from
what I said earlier in the thread. I think someone is quoting the wrong
poster.
 
C

Chris Uppal

EJP wrote:

[me:]
Overstating? everybody else thought I was understating! I got it down to
'type-generic code' (not my term): wasn't that far enough?

Perhaps I misunderstood you. Looking back, you certainly started out in the
"right" place:

[are generics similar to templates ?]
Not really, no, except in *syntax*.
C++ templates are a code-generation mechanism. Java Generics are a
type-constraint system which generate no code at all.

But then seemed to dilute your position to allow that there was some meaningful
conceptual similarity -- which I would dispute.

My apologies if I misrepresented you.

-- chris
 
D

Darryl L. Pierce

EJP said:
I think it's more than just implementation actually. C++ templates are
*conceptually* associated with the idea of 'instantiation' per type.

And, as you said, C++ templates generate new code. Java generics do not.
That's an implementation detail that completely separates the two
otherwise conceptual similar systems.
 
E

EJP

Darryl said:
And, as you said, C++ templates generate new code. Java generics do not.
That's an implementation detail that completely separates the two
otherwise conceptual similar systems.

Code generation is carried out for and only for the purpose of template
instantiation, and as I said instantiation is a conceptual attribute not
just an implementation attribute, so I disagree.
 
D

Darryl L. Pierce

EJP said:
Code generation is carried out for and only for the purpose of template
instantiation,

Does the template specification say that that is what templates are
intended to do? Or is it simply the mechanism for fulfilling the
specification?
 
E

EJP

Darryl said:
Does the template specification say that that is what templates are
intended to do? Or is it simply the mechanism for fulfilling the
specification?

From the ARM:

'A class template defines the layout and operations for an unbounded set
of related types [BTW 'related' is a dubious claim] ... a single
function template sort() might define a common definition for sorting
all the types defined by the List class template ... A template defines
a family of types or operations ... The template mechanism ... allows
close to optimal run-time performance through macro expansion of
definitions ... A class template specifies how individual classes may be
constructed much as a clas declaration specifies how individual objects
cabn be constructed ... a class generated from a class template is
called a template class ... here, vector<int> and vector<complex> are
template classes, and their definitions will by default be generated
from the vector template ...'

I could go on. It's entirely clear that C++ templates *generate new
types*. Every template class is a new class, and the template itself is
not a class at all. A Java generic is a type-constrained notation for a
single class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,065
Members
48,769
Latest member
Clifft

Latest Threads

Top