Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

J2SE 5.0 Generics and C++ Templates

Thread view: 
sarathy - 06 Aug 2006 21:15 GMT
Hi all,
            Is there any relation between the J2se 5.0 generics and
C++ templates ??

Regards,
Sarathy
Darryl L. Pierce - 06 Aug 2006 21:26 GMT
>              Is there any relation between the J2se 5.0 generics and
> C++ templates ??

Not really, no, except in concept.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard

--
Posted via a free Usenet account from http://www.teranews.com

EJP - 07 Aug 2006 00:33 GMT
>>              Is there any relation between the J2se 5.0 generics and
>> C++ templates ??
>
> 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.
Darryl L. Pierce - 07 Aug 2006 12:01 GMT
>>> Is there any relation between the J2se 5.0 generics and C++
>>> templates ??
[quoted text clipped - 4 lines]
>
> 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.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard

--
Posted via a free Usenet account from http://www.teranews.com

EJP - 08 Aug 2006 01:45 GMT
>> C++ templates are a code-generation mechanism.
>
> 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.
Mark Space - 08 Aug 2006 07:12 GMT
> 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?
EJP - 08 Aug 2006 08:22 GMT
> So: type generic code, and angle brackets.  That's it?

I think so, and to be honest I wish Java didn't have the angle brackets.
Chris Uppal - 08 Aug 2006 10:42 GMT
> 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
Jerry Coffin - 08 Aug 2006 19:22 GMT
> > 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"...

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.

Signature

   Later,
   Jerry.

The universe is a figment of its own imagination.

EJP - 09 Aug 2006 01:22 GMT
>>I think so, and to be honest I wish Java didn't have the angle brackets.
>
> 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?
Michael Safyan - 09 Aug 2006 02:48 GMT
>>> I think so, and to be honest I wish Java didn't have the angle brackets.
>>
[quoted text clipped - 3 lines]
>
> 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.
EJP - 09 Aug 2006 03:11 GMT
>>>> I think so, and to be honest I wish Java didn't have the angle
>>>> brackets.
[quoted text clipped - 13 lines]
> 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.
Chris Uppal - 09 Aug 2006 10:40 GMT
[me:]
> > 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?

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
Mark Space - 08 Aug 2006 18:49 GMT
>> So: type generic code, and angle brackets.  That's it?
>
> I think so, and to be honest I wish Java didn't have the angle brackets.

*chuckles...*
Richard Smol - 14 Aug 2006 21:46 GMT
> > So: type generic code, and angle brackets.  That's it?
>
> I think so, and to be honest I wish Java didn't have the angle brackets.

I agree. Those brackets are darn ugly.

RS
Darryl L. Pierce - 09 Aug 2006 12:11 GMT
>>> C++ templates are a code-generation mechanism.
>>
>> 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.

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.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard

--
Posted via a free Usenet account from http://www.teranews.com

EJP - 10 Aug 2006 05:03 GMT
> 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.
Darryl L. Pierce - 10 Aug 2006 11:56 GMT
>> 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,

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

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard

--
Posted via a free Usenet account from http://www.teranews.com

EJP - 11 Aug 2006 03:03 GMT
>>> And, as you said, C++ templates generate new code. Java generics do
>>> not. That's an implementation detail that completely separates the
[quoted text clipped - 6 lines]
> 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.
Darryl L. Pierce - 12 Aug 2006 11:59 GMT
> 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 wrote:
>              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...

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard

--
Posted via a free Usenet account from http://www.teranews.com

EJP - 13 Aug 2006 06:32 GMT
> 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.
Darryl L. Pierce - 13 Aug 2006 13:42 GMT
>> 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
[quoted text clipped - 3 lines]
> don't understand how you think that this difference isn't both
> conceptual and fundamental.

Whatever.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard

--
Posted via a free Usenet account from http://www.teranews.com

Chris Smith - 14 Aug 2006 20:21 GMT
> 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.

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

Michael Redlich - 07 Aug 2006 12:11 GMT
>              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.

---
ACGNJ Java Users Group
http://www.javasig.org/
---
Chris Smith - 07 Aug 2006 16:33 GMT
> 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.

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.