www.digitalmars.com         C & C++   DMDScript  

D - non-static member-template-function proposal

reply Matthias Becker <Matthias_member pathlink.com> writes:
One of the last features I realy miss in D are non-static
member-template-function. I think they aren't there because it's very hard or
nearly impossible to make membertemplate-functions virtual (virtual in terms of
C++). So I suggest to implement them as non-virtual methods. It can't be very
hard to implement them this way, but they would be very usefull.
Jan 16 2004
parent reply "Achilleas Margaritis" <axilmar b-online.gr> writes:
Yes, indeed. Template methods are very important. For example, in  C++, I
have made a Variant class which uses template methods to hold any type of
variable. In the template method, a template class that holds the variable
is created. I use the Variant class for moving around data without knowing
their data type, even from DLLs.

boost::any also uses this (I think!!!).

Why doing virtual template methods are hard ? I don't buy it. A template is
simply a parameterized function, right ? so, if the subclasses overloads the
method like it is (that means, as a template), then all instantiations are
virtual.

"Matthias Becker" <Matthias_member pathlink.com> wrote in message
news:bu8h1u$3jl$1 digitaldaemon.com...
 One of the last features I realy miss in D are non-static
 member-template-function. I think they aren't there because it's very hard
or
 nearly impossible to make membertemplate-functions virtual (virtual in
terms of
 C++). So I suggest to implement them as non-virtual methods. It can't be
very
 hard to implement them this way, but they would be very usefull.
Jan 16 2004
parent reply Matthias Becker <Matthias_member pathlink.com> writes:
Why doing virtual template methods are hard ? I don't buy it. A template is
simply a parameterized function, right ? so, if the subclasses overloads the
method like it is (that means, as a template), then all instantiations are
virtual.
Imagine this: class Base { template <typename T> // c++-style pseudocode virtual void foo (T x) { ... } }; class DerivedA : public Base { template <typename T> void foo (T x) { ... } }; class DerivedB : public Base { template <typename T> void foo (T x) { ... } }; Base * foo = new DerivedA; foo->foo(22); // when the compiler sees this it has to add an entry to the VMT of Base, as well as to DerivedA which is obvious, but what about DerivedB? it isn't involved here, but anyway it is derived from Base, so it's VMT needs an entry for this method.
Jan 17 2004
next sibling parent Andy Friesen <andy ikagames.com> writes:
Matthias Becker wrote:
 
 Imagine this:
 
 [...]
 
 foo->foo(22); // when the compiler sees this it has to add an entry to the VMT
 of Base, as well as to DerivedA which is obvious, but what about DerivedB? it
 isn't involved here, but anyway it is derived from Base, so it's VMT needs an
 entry for this method.
 
So how about if all template methods had to be either static or final? No vtable entry required, right? -- andy
Jan 17 2004
prev sibling parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
Maybe I'm having a thick day, but I don't see the problem. Can you
re-explain? :)

"Matthias Becker" <Matthias_member pathlink.com> wrote in message
news:bubbsm$1si9$1 digitaldaemon.com...
Why doing virtual template methods are hard ? I don't buy it. A template
is
simply a parameterized function, right ? so, if the subclasses overloads
the
method like it is (that means, as a template), then all instantiations
are
virtual.
Imagine this: class Base { template <typename T> // c++-style pseudocode virtual void foo (T x) { ... } }; class DerivedA : public Base { template <typename T> void foo (T x) { ... } }; class DerivedB : public Base { template <typename T> void foo (T x) { ... } }; Base * foo = new DerivedA; foo->foo(22); // when the compiler sees this it has to add an entry to the
VMT
 of Base, as well as to DerivedA which is obvious, but what about DerivedB?
it
 isn't involved here, but anyway it is derived from Base, so it's VMT needs
an
 entry for this method.
Jan 17 2004