www.digitalmars.com         C & C++   DMDScript  

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

reply Ben Phillips <Ben_member pathlink.com> writes:
[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 ;)
Feb 20 2006
parent reply Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
Ben Phillips wrote:

 instead of having to write "something!(int).someFunction(i)" (which seems
 excessively verbose for one function) you could write someFunction!(int)(i)
You can do this if you name the template the same as the function. This has nothing to do with the template being defined by the short or long form. Read under Implicit Template Properties in the docs. /Oskar
Feb 20 2006
parent Ben Phillips <Ben_member pathlink.com> writes:
In article <dtcrdp$2529$1 digitaldaemon.com>, Oskar Linde says...
Ben Phillips wrote:

 instead of having to write "something!(int).someFunction(i)" (which seems
 excessively verbose for one function) you could write someFunction!(int)(i)
You can do this if you name the template the same as the function. This has nothing to do with the template being defined by the short or long form. Read under Implicit Template Properties in the docs. /Oskar
Thanks for the heads up! I missed that.
Feb 20 2006