D's templates now seem weak to me...

Ben Phillips Ben_member at pathlink.com
Mon Feb 20 08:28:32 PST 2006


[sorry for incomplete post before]

According to the docs, "Templates cannot be used to add non-static members or
functions to classes. For example: [...]". Why? This is just plain stupid in my
opinion. I wanted to write a general purpose allocator, but found that D makes
it impossible (unless I am really missing something).

A trivial C++ example of how to solve my problem:
class allocator
{
public:
allocator() { }

template<typename T>
T* allocate()
{
return new T;
}

template<typename T>
void release(T* ptr)
{
delete ptr;
}
};

I can't see any way to do this in D, which I find extremely annoying. I don't
think it should be necessary to resort to having to write allocators that use
"malloc" instead of "new", but I can't see any other reasonable option. I
considered going back to the crappy C++ allocators that have to be specialized
for allocating a specific type, but I decided not to. Why? Because I want an
allocator that can be used to allocate different types without having to use
"rebind" or allocate another allocator.

Another problem I have with D's templates is that I can't see a clean way to
create a template function.

Yes D supports,
template something(Type)
{
void someFunction(Type t) { ... }
}

but why not something simpler like
template(Type) void someFunction(Type t) { ... }

instead of having to write "something!(int).someFunction(i)" (which seems
excessively verbose for one function) you could write someFunction!(int)(i)
(much cleaner imo). I know I could use aliases but why think up 3 names when its
easier just to think up 1?

Thats my rant. I'm not as aggravated as I sound ;)





More information about the Digitalmars-d mailing list