Just remember that ages ago the orginal C++ language was implemented as something like a preprocessor that produced straight C code…
Apples and oranges Scott. There are many languages which use C as their IL (intermediate language). Heck, there are static Java compilers which use C as their IL. Are you suggesting that Java is just a macro? The key here is that the process is actually compilation - which all of us CS geeks know is just a synonym for translation.
much like a macro
Fine. You’ve taken what my point was (Templates, like java.lang.reflect.Proxy, are a form of generative-generic programming), and turned it on its head. Let me ask you this, then: Do you believe java.lang.reflect.Proxy is basically a macro? If not, then how is it different? Then how does Proxy differ from templates? Hopefully, these questions will be rhetorical for you.
that ‘meta-programming’ sounds a lot like an abusive hack…
It’s definitely not a hack (templates were designed to have those capabilities), but it’s not exactly pretty either. It’s the preferred way, now, because it is currently the most elegant solution. I would love to see Java implement the same functionality, but in a much more elegant fashion.
And, I unroll loops with Ctrl-C, Ctrl-V all the time, so I seem to have missed the point there.
Yes, you strayed far from the point. You can use templates to create a parameterized algorithm which the compiler manually unrolls for you. For example, you can create a matrix algorithm that will automatically unroll itself perfectly for any size matrix. With manual unrolling, you would have to create a new version of the algorithm for each differently sized matrix. If you needed to change your algorithm, you’d have to do it for each and every unrolled loop for each and every differently sized matrix. Hope your wife won’t miss you while your gone.
These things certianly don’t sound like the stuff that I see C++ templates used for 99.9% of the time.
It is common. Check out OONumerics and the Loki library for example.
So the fact that the Java Generics mechanism can’t handle that specific bit doesn’t seem like a big deal.
Humm… what you’ve done here is falsely trivialized what templates can do, and then said that you don’t see why they are good.
As another example of what templates can do, consider specialization. I can take any algorithm and specialize it for any data type, and it’s totally transparent to the users of that algorithm. Jace (my open-source C++ JNI library) uses template specialization extensively throughout its library to create seamless access to Java arrays, fields, and methods of any type.
In terms of how I see C++ templates used, they are essentially a glorified macro - a really powerful and useful one, sure.
Actually, if anything is like a macro, it’s Java generics. Think about it some. One is a Turing complete language - the other drops in type casts.
Well I certainly agree that if Java Generics are nothing more than hiding some casting, then they perhaps aren’t as powerful as they should be. Although if you were going to use things like the generic containers and casting anyway they do make things easier. So while java Generics aren’t what you want them to be, they are still good for something.
Right - they’re mostly useless. Somewhat more powerful than comments.
Could you implement something that appeared to be the equivalent of STL with java generics for instance?
Somebody has already written a poor Java imitation of the standard C++ container library - It’s called JGL. It, for example, doesn’t support specialization. It also doesn’t implement anything akin to traits classes. It also has no concept of policy classes. All of these things come from templates.
'cause that to me seems to cover the bulk of what C++ templates are used for.
So, I’ve already covered what you missed in the standard C++ library alone. Again, I suggest you take a look at some powerful template libraries. Check out Loki, Blitz++, the C++ Lambda library, even Jace, for real world examples of the many powerful things you can do with templates.
God bless,
-Toby Reyelts