Original Post
Does Java support generic programing? Is there a way to implement C++ style templates in Java?
-Mike
quote:
Original post by c_wraith
Java doesn''t, and never will, support anything even close to C++ templates. However, version 1.5, due for beta release in a year or so, will support something that looks kind of like C++ templates in syntax and use...
The implementation won''t be anything like what C++ uses, though. Specifically, it will just be a mechanism for improving clode clarity and compile-time type checking. It won''t have any of the other uses C++ templates have.
As for whether generic programming is possible in java: somewhat. It really depends on what you mean by generic programming. If you want to create a data structure that can hold any kind of object (but not primitives), that can be done. If you want to use any of templates'' other uses, you''ll need to find another way.
quote:
Original post by Dobbs
A lot of C++ template use is just for generic data containers anyway.
quote:
Original post by Dobbs
...Althought that was the intent
when templates were added to C++, some very clever
people found use of templates to do compile-time programming,
or template metaprogramming. Pretty powerful stuff if you
ask me...
quote:
Original post by nicho_tedja
Would you be so kind to share it with us?
quote:Here''s a compile-time fibonacci number calculator. The parameter to fib() needs to be a constant known at compile time.
Original post by nicho_tedja
Would you be so kind to share it with us?![]()
#define fib(x) (MUHAHA<x>::value)
template <int x>
struct MUHAHA {
static const int value = fib(x - 1) + fib(x - 2);
};
template <>
struct MUHAHA<1> {
static const int value = 1;
};
template <>
struct MUHAHA<2> {
static const int value = 1;
};
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more