Java/C++ generics/templates

I’ve read today this blog entry about java/c++ performance .

i agree with most of his arguments, but the generics one… well…

i wanted to post a comment on his blog, but it kept asking me to login when i wanted to post a comment, and i couldn’t find a way to register, so i am posting my “reply” here:

where he argues about java/c++ generics/templates, he says that the java ones are basically better. while it might be true, that there are certain enhancements in the way java does templates, we must not forget, that there are certain very useful things one can do with templates, but cannot with generics.

example

#include <iostream>
#include <string>
 
using namespace std;
 
class Clazz1
{
    public:
    string get_hello_message();
};
 
string Clazz1::get_hello_message()
{
    return string("Clazz1.hello");
}
 
 
class Clazz2
{
    public:
    string get_hello_message();
};
 
string Clazz2::get_hello_message()
{
    return string("Clazz2.hello");
}
 
 
 
template <class T>
T say_hello(T obj)
{
    cout < < obj.get_hello_message() << endl;
}
 
int main()
{
Clazz1 c1;
Clazz2 c2;
say_hello(c1);
say_hello(c2);
 
return 0;
}

in this C++ program i define 2 classes, both have a method called get_hello_message, but the two classes are not related to each other. then i define a function, that takes an object of any type (and calls it’s get_hello_message method.

try this in java :)

Trackback URL for this post:

http://www.nekomancer.net/trackback/146