[OT] Java, C# : no need for templates?

C

crichmon

Hi,

The primary programming language that I use is C++. I have done some
reading and found that neither Java or C# have templates. Is this due to
everything deriving from a common Object class? Am I incorrect in my
awareness? How do Java and C# developers replace templates in their
programs?

thanks,
crichmon
 
R

red floyd

crichmon said:
Hi,

The primary programming language that I use is C++. I have done some
reading and found that neither Java or C# have templates. Is this due to
everything deriving from a common Object class? Am I incorrect in my
awareness? How do Java and C# developers replace templates in their
programs?

thanks,
crichmon

Don't know about C#, but the next version of Java will have generics.
 
I

Ioannis Vranos

Russell said:
C# 2.0 will also have "generics." See http://tinyurl.com/2okbm


C++ will also have generics in .NET and *standardised* in the C++/CLI
binding standard by ECMA (and thus in all garbage collection
environments that will support C++). What are the major differences
between templates and generics?

Templates are instantiated at compile time while Generics at run-time.
Also Generics do not allow specialisations.


In case you haven't understood it, C++ is getting in virtual machines
applications too. :)


Check these:


There check the Visual C++ paragraph:
http://msdn.microsoft.com/vstudio/productinfo/roadmap.aspx#language


http://www.ecma-international.org/news/ecma-TG5-PR.htm


http://msdn.microsoft.com/visualc/homepageheadlines/ecma/default.aspx



Also C++ will be able to use templates (compile time) in .NET 2.0 and
later. For example a vector<Button>barray(10);


Also it will be able to create managed objects in the stack as usual
objects.


Anyway, C++ future is promising.






Regards,

Ioannis Vranos
 
C

crichmon

Pete Becker said:
Cut and paste.

Ug. I once worked on a project coded in C and the projected needed linked
lists for 10+ types. The cut-and-paste method was a major pain IMO!

crichmon
 
I

Ioannis Vranos

Ioannis said:
C++ will also have generics in .NET and *standardised* in the C++/CLI
binding standard by ECMA (and thus in all garbage collection
environments that will support C++). What are the major differences
between templates and generics?

Templates are instantiated at compile time while Generics at run-time.
Also Generics do not allow specialisations.


In case you haven't understood it, C++ is getting in virtual machines
applications too. :)


Check these:


There check the Visual C++ paragraph:
http://msdn.microsoft.com/vstudio/productinfo/roadmap.aspx#language


http://www.ecma-international.org/news/ecma-TG5-PR.htm


http://msdn.microsoft.com/visualc/homepageheadlines/ecma/default.aspx



Also C++ will be able to use templates (compile time) in .NET 2.0 and
later. For example a vector<Button>barray(10);


Also it will be able to create managed objects in the stack as usual
objects.


Anyway, C++ future is promising.



And to give some more info on C++/CLI standard, here is some sample code:


// Garbage collected class
ref class whatever
{
// ...
};


whatever ^p = gcnew whatever;


// No need to call delete


long l=7;

long *lp=new long(7);

// ...

delete lp;


long ^lp=gcnew long(7);



// Operator overloading as usual for GC pointers
whatever^ operator+(whatever ^a, whatever ^b)
{
// ...
}


vector<int>*p = new vector<int>;

// ...

delete p;


// This vector is garbage collected
// We can make any object garbage collected
vector<int>^pvec = gcnew vector<int>;



So you see the use of garbage collection in C++ will be seamless with
the upcoming C++/CLI standard.






Regards,

Ioannis Vranos
 
C

crichmon

Ioannis Vranos said:
C++ will also have generics in .NET and *standardised* in the C++/CLI
binding standard by ECMA (and thus in all garbage collection
environments that will support C++). What are the major differences
between templates and generics?

Templates are instantiated at compile time while Generics at run-time.
Also Generics do not allow specialisations.

Do you mean by that last statement that a class will not be able to inherit
from a generic? like

class MessageCenter: public Singleton<MessageCenter>
{
....
}

If not could you elaborate?

In case you haven't understood it, C++ is getting in virtual machines
applications too. :)


Check these:


There check the Visual C++ paragraph:
http://msdn.microsoft.com/vstudio/productinfo/roadmap.aspx#language


http://www.ecma-international.org/news/ecma-TG5-PR.htm


http://msdn.microsoft.com/visualc/homepageheadlines/ecma/default.aspx



Also C++ will be able to use templates (compile time) in .NET 2.0 and
later. For example a vector<Button>barray(10);


Also it will be able to create managed objects in the stack as usual
objects.


Anyway, C++ future is promising.

Excellent! I look forward to it.

crichmon
 
I

Ioannis Vranos

crichmon said:
Do you mean by that last statement that a class will not be able to inherit
from a generic? like

class MessageCenter: public Singleton<MessageCenter>
{
....
}

If not could you elaborate?



I have not much knowledge of C++/CLI standard so I am not sure 100%, but
I think inheritance is allowed for generic classes.


I was talking about specialisations, for example define your own vector
"generic" class, and try to create a specialisation for bool. You can't
do such a specialisation with generics. And I think this is because of
the fact that generics are instantiated at run-time so it probably needs
to have only one definition in place.


However you can create a template class and create its objects on the GC
free store, or use GC handles (^) in it too.






Regards,

Ioannis Vranos
 
I

Ioannis Vranos

Ioannis said:
I have not much knowledge of C++/CLI

draft

standard so I am not sure 100%, but
I think inheritance is allowed for generic classes.


I was talking about specialisations, for example define your own vector
"generic" class, and try to create a specialisation for bool. You can't
do such a specialisation with generics. And I think this is because of
the fact that generics are instantiated at run-time so it probably needs
to have only one definition in place.


However you can create a template class and create its objects on the GC
free store, or use GC handles (^) in it too.






Regards,

Ioannis Vranos
 
A

Alexei

Ioannis Vranos said:
Templates are instantiated at compile time while Generics at run-time.
Also Generics do not allow specialisations.

Let me expand on this a little. Generics are actually instantiated at
compile time, i.e. when the compilation from the intermediate language (IL)
to machine code happens. It may occur at runtime (JIT), or it can be
compiled ahead of time (pre-compiled with NGEN).

One major difference between generics and templates (in my opinion) is that
parameter types for generics are assumed to have methods only of Object type
(the base of all types) unless specified otherwise with "constraints".
Whereas parameter types for templates are assumed to have any methods
possible. This key difference makes it impossible to use generics for
generic meta-programming.

Ultimately (again IMHO) the above restriction for generics is the
consequence of its implementation. The major problem which was tried to be
avoided when generics were designed is code bloat. I'm judging by early
articles where the point of code bloat was emphasized frequently. So the
..NET designers decided to merge generic instantiations where parameter types
are derived from Object. Also the fact that all coding errors have to be
caught when the code is compiled into IL (versus when IL is compiled into
machine code), forces IL to be well defined/unambiguous. This makes
impossible for generic parameter types to have methods other than of Object
type, because Object is the least common denominator of all types in .NET.

So as I say avoiding code bloat seems to be one of the major reasons for
restricting "genericity" of generics. But there is a little irony here.
The "code bloat" argument was exploited so much by early articles on C# v2,
that it might seem C# does a better job in this respect then C++. But this
is not true. The sacrifice was for nothing! C++ does better job with code
bloat than C#. And here is how. In C# generic instantiations with
parameters of "value" types (types which are allocated on the stack) cannot
be merged. That is. List<int> and List<uint> will result in duplicating
the code of the generic class List<T> for type int and uint. But C++ is
smarter in this respect. Not only C++ linker merges template instantiations
for pointer type parameters (pointer is a rough equivalent of Object type in
C#), but it also merges _any_ methods with identical code. So in the above
example, code for List<int> and List<unsigned> template instantiations will
be merged if identical.

I won't laugh at C#/.NET guys for promoting generics over templates on the
merits of better handling the code bloat. Designing virtual machines is a
hard job. But the irony still stands.
 
M

Martin Demberger

Am Sat, 03 Jul 2004 00:39:03 GMT schrieb crichmon:
Hi,

The primary programming language that I use is C++. I have done some
reading and found that neither Java or C# have templates. Is this due to
everything deriving from a common Object class? Am I incorrect in my
awareness? How do Java and C# developers replace templates in their
programs?

thanks,
crichmon

I don't know C#. But in Java you use Object. Thats the Superclass of all
other classes.
This means if you have in C++ a

vector<Customer> v;
v.push_back(c);
c = v[0];

You will use in Java a

List v = new ArrayList();
v.add(c); // Implicite cast to Object
c = (Customer)v.get(0);

This is like void* in C. You can downcast all to this Type and if you get
it out of the List you upcast it to its real Datatype.

A special usage is for so called primary datatypes. These are boolean,
char, short, int, long, float and double.
They aren't inherited from Object. So there is a wrapper-object with the
same name but in Uppercase: Boolean, Character, Short, Integer, Long, Float
and Double.
Integer i = new Integer(42)

You lose type-safety with this concept but that isn't realy a problem if
you know whats in your List.
 
I

Ioannis Vranos

Alexei said:
Let me expand on this a little. Generics are actually instantiated at
compile time, i.e. when the compilation from the intermediate language (IL)
to machine code happens. It may occur at runtime (JIT), or it can be
compiled ahead of time (pre-compiled with NGEN).


I do not know what is NGEN but I assume you mean during installation by
that. However when we are talking about compilation, we mean from source
code to IL code (when we "compile" our program to produce the .exe).


The compilation from IL to machine code is responsibility of the virtual
machine and not the "compiler" which produces the IL (bytecode).



One major difference between generics and templates (in my opinion) is that
parameter types for generics are assumed to have methods only of Object type
(the base of all types) unless specified otherwise with "constraints".
Whereas parameter types for templates are assumed to have any methods
possible. This key difference makes it impossible to use generics for
generic meta-programming.


Draft standard terminology:

"System::Object, which is the ultimate base type of all handle types"


Ultimately (again IMHO) the above restriction for generics is the
consequence of its implementation. The major problem which was tried to be
avoided when generics were designed is code bloat. I'm judging by early
articles where the point of code bloat was emphasized frequently. So the
..NET designers decided to merge generic instantiations where parameter types
are derived from Object. Also the fact that all coding errors have to be
caught when the code is compiled into IL (versus when IL is compiled into
machine code), forces IL to be well defined/unambiguous. This makes
impossible for generic parameter types to have methods other than of Object
type, because Object is the least common denominator of all types in .NET.



Better let's talk portably using the draft standard, since there is Mono
for example.

In Mono the above apply too.



So as I say avoiding code bloat seems to be one of the major reasons for
restricting "genericity" of generics. But there is a little irony here.
The "code bloat" argument was exploited so much by early articles on C# v2,
that it might seem C# does a better job in this respect then C++. But this
is not true. The sacrifice was for nothing! C++ does better job with code
bloat than C#. And here is how. In C# generic instantiations with
parameters of "value" types (types which are allocated on the stack) cannot
be merged. That is. List<int> and List<uint> will result in duplicating
the code of the generic class List<T> for type int and uint. But C++ is
smarter in this respect. Not only C++ linker merges template instantiations
for pointer type parameters (pointer is a rough equivalent of Object type in
C#), but it also merges _any_ methods with identical code. So in the above
example, code for List<int> and List<unsigned> template instantiations will
be merged if identical.

I won't laugh at C#/.NET guys for promoting generics over templates on the
merits of better handling the code bloat. Designing virtual machines is a
hard job. But the irony still stands.



Well in C++ efficiency has always been a major design ideal. Thanks for
the additional information you provided.






Regards,

Ioannis Vranos
 
D

Dietmar Kuehl

Russell said:
C# 2.0 will also have "generics." See http://tinyurl.com/2okbm

Neither of those is even close to C++ templates: both forms of generics
are exclusively targeted at type-safe containers. With neither Java nor
C# generics you can create generic algorithms (essentially because in
both cases there is no way to infer associated types).

Actually, with respect to generics both languages are where C++ has been
with templates something like 15 years ago. The designers of both
languages refused to learn from C++. ... and I doubt that they will
improve the system to something usable any time soon!
 

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,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top