J2SE 5.0 Generics and C++ Templates

D

Darryl L. Pierce

EJP said:
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.

And it still an implementation detail of how the concept (about which is
what I spoke originally) is fulfilled, the concept being to write code
in a very generic way. C++ generates a whole new class while Java
doesn't, but that's immaterial to them both sharing the same concept. So
my original statement:

---8<[snip]---
sarathy said:
> Is there any relation between the J2se 5.0 generics and
> C++ templates ??

Not really, no, except in concept.
---8<[snip]---

is spot on true. I don't understand why you think that C++ generating a
new type while Java doesn't somehow means that conceptually the two
aren't at all similar...
 
E

EJP

Darryl said:
I don't understand why you think that C++ generating a
new type while Java doesn't somehow means that conceptually the two
aren't at all similar...

Well I don't, and apparently neither does anyone else here. In fact I
don't understand how you think that this difference isn't both
conceptual and fundamental. The 'similarity' is so superficial as to be
merely syntactical, as I said originally, or to amount to no more than a
way of doing some kind of type-generic programming, as I said later and
was castigated for, probably rightly.
 
D

Darryl L. Pierce

EJP said:
Well I don't, and apparently neither does anyone else here. In fact I
don't understand how you think that this difference isn't both
conceptual and fundamental.

Whatever.
 
C

Chris Smith

EJP said:
In fact I
don't understand how you think that this difference isn't both
conceptual and fundamental. The 'similarity' is so superficial as to be
merely syntactical, as I said originally, or to amount to no more than a
way of doing some kind of type-generic programming, as I said later and
was castigated for, probably rightly.

I wasn't following the thread too closely, so I didn't noticed your
being catigated, nor what you were castigated for. I actually think you
aren't going far enough. Saying that Java generics are a way of doing
type-generic programming is incorrect. Inheritance and polymorphism as
a singly-rooted inheritance hierarchy are a way of doing generic
programming. Generics are a way of adding type system awareness to
type-generic programming.

In STL algorithms, for example, comparison is done with an overloaded
'<' operator or with a functor, both using templates. In Java, the same
thing is done with ordinary objects that implement interfaces
(Comparable or Comparator) and provide code that is called via
polymorphic dispatch. Java generics do not change that fact at all.
What they do is provide an expressive enough type system that the
compiler can remain aware of types while it evaluates generic
programming operations.

Therefore, a comparison between templates and generics is somewhat
poorly founded, not just because they work differently, but also because
they solve different problems. C++ templates provide a very broad
mechanism for solving a variety of problems. Java's generics are more
precisely aimed at a specific problem -- merely expressing the typing
relation -- and therefore look considerably different aside from the
angle-brackets.
 

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,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top