[TriLUG] C++ templates and Java generics (ontopic for both lists)

Tanner Lovelace lovelace at wayfarer.org
Mon Jul 1 17:23:37 EDT 2002


On Mon, 2002-07-01 at 16:58, Andrew C. Oliver wrote:

> *sigh* - yes I understand that in C++ they are compile-time.  Yes I 
> understand C++ and yes I like
> templates and have used them significantly in the past.  I also 
> understand that Java Generics does not
> provide to Java 1-1 what they do to C++.  By the nature of the lanugage, 
> while EVERYTHING in C++
> is compile time (more or less) that does not mean the same thing should 
> be compile-time in Java.  Java
> Generics should not be compile-time.  
> 
> (the sigh was at the fact I get tired of "gee if you don't like it you 
> must not understand it")

Andy, I didn't even think as to whether you liked it or not.
My thought that you didn't understand it came directly from
your statement about them, which seemed to indicate you
thought C++ templates were not compile time.  I guess I misunderstood
you, and for that I apologize.
 
> As to why it won't support them, it has largely to do with the fact that 
> they'll be compile-time (now you're
> getting at the crux of the issue).  You cannot have a int$1.class 
> because int is not a class.  (And hence the core
> of why they should not be compile-time).  Because of the way Java works, 
> while compile-time is appropriate
> here for C++, this shows quite easily why it is not necessarily 
> appropriate in this area for Java.
> 
> The Second rationale on why it won't support primitives, is even if it 
> expanded the code to wrap "int" in Integer, there
> would be the same performance -- as just having the programmer doing the 
> same "boxing/unboxing" manually.
> 
> (I'm explaining the rationale.....this is not "I agree with it" -- and 
> yes I understand...but do not disagree it)
> 
> complete agreement on "typedef".
> 


> And now you get into an area I do not profess to understand (notice my 
> blatant admission of
> "I don't understand" versus "I don't like").  I'll guess at what that 
> would mean to C++
> 
>  From my admitedly partial undestanding of  "templates of templates" and 
> partial "specialization", and my
> good understanding of Java Generics, I would guess not.  (explanation below)

Template of templates is just how I meant to say you can
pass a template as a parameter into another template.

Specialization is when you have a generic algorithm 
that works well *most* of the time, but there are a few cases
where it doesn't work as good as a specialized algorithm.  In
this case, you basically tell C++ that if the user instantiates
a template with this particular type, use the specialized
algorithm the programmer has provided for that particular type.
Specialization can either be full (i.e. all the types of
the template) or partial (i.e. if a template has two parameters
and the programmer provides a version when one of them is a 
specific type).

Specialization actually allows C++ templates to be, in and of
themself, Turing complete.  This was brought to the C++ committee's
attention when someone happened to walk into their meeting
with a program that computed prime numbers in the output
of it's compiler error messages!  The Blitz library makes
good use of this.

> You're turning this argument into something I'm not arguing: against 
> generics entirely.
> Java needs generics but generics needs to support primitive types.  In 
> Java this would
> mean run-time support for generics (not just compile-time).  My argument 
> is that the
> proposed JSR-014 is nothing but syntax candy.  
> 
> And I would guess (although I haven't tried it) that in this 
> implementation, debugging generics will most decidedly
> not be an enjoyable experience.  While this is admittedly an 
> oversimplification, generics will
> be implemented as a:
> 
> Class containing a "magic inner class selector"
> a set of implied inner classes
> 
> -- This won't match what you typed of course!
> 
> I cannot imagine that this will be straightforward to debug, perhaps it 
> will be mitigated by tomorrow's
> debugger.

Well, as I have admittedly only seen a very early version of 
Java generics, I can't speak to this.  I would say, however,
that even what you seem to call "syntax candy" will help 
with early detection of a certain class of errors.
 
Tanner
-- 
Tanner Lovelace | lovelace at wayfarer.org | http://wtl.wayfarer.org/
--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--
GPG Fingerprint = A66C 8660 924F 5F8C 71DA  BDD0 CE09 4F8E DE76 39D4
GPG Key can be found at http://wtl.wayfarer.org/lovelace.gpg.asc
--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--
"Computer games don't affect kids; I mean if Pac-Man affected us as
kids, we'd all be running around in darkened rooms, munching magic
pills and listening to repetitive electronic music."
                                Kristian Wilson, Nintendo, Inc, 1989




More information about the TriLUG mailing list