class scope virtual template functions

BCS ao at pathlink.com
Thu May 29 11:02:21 PDT 2008


Somewhere IIRC the spec says that classes can't have template methods that 
operate as virtual functions. This is because it would quickly become a nightmare 
trying to put together the v-table because only the linker would really know 
how many different function are in it.

However in highly limited cases, this is not an issue. 

1) the template members are enumerations (just build the whole set)
2) the function has no parameters (only one version can be built)
3) no general form of the template exists, only explicit specializations 
(ditto enum)
4) class scope aliases of member templates are used. (instance the template 
and stuff it in as a normal function)

There is also an interesting possibility suggested by #2: allow a template 
with no args that is penalized for the actual type of the specific object

Example:

class A
{
  char[] Name(this)()  // "this" is a pseudo arg
  { return typeof(this).stringof; }
}
class B : A {}

writef("%s\n", (new A).Name); // outputs "A"
writef("%s\n", (new B).Name); // outputs "B"

Thoughts?

I have some thoughts on how to use this functionality, but it's to ill-formed 
at this time to fit in this margin.





More information about the Digitalmars-d mailing list