www.digitalmars.com         C & C++   DMDScript  

D - Template Limitations

reply Colin JN Breame <Colin_member pathlink.com> writes:
I've been reading the template section in the reference manual.  It says:

"Multiple instantiations of a TemplateDeclaration with the same
TemplateParameterList all will refer to the same instantiation."

Is this a feature or a flaw?

"Templates cannot be used to add non-static members or functions to classes."

Is this something that will be rectified in the future?

I'm I right in saying that you can add members and functions to a struct using a
template?  If so, is it the case that there will only be one instance of that
struct?

Thanks
Apr 23 2004
next sibling parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Colin JN Breame schrieb:
 I've been reading the template section in the reference manual.  It says:
 
 "Multiple instantiations of a TemplateDeclaration with the same
 TemplateParameterList all will refer to the same instantiation."
 
 Is this a feature or a flaw?
I think you misunderstand this phrase. Templates generate code. This feature guarantees that not multiple unrelated pieces of exactly same mode are created for each module etc, but that doesn't matter how often you instantiate a template, as long as it's one set of parameters, it reuses this code over and over. This doesn't restrict you in any way, e.g. you make a template of a class which takes one type argument, then you can create multiple instances of it with an argument of (int), then make multiple class instances of these, but all instances will still have the same ClassInfo, meaning that they belong to the same class. If it weren't so, the templates would be nearly useless, and bloat would be unbound.
 "Templates cannot be used to add non-static members or functions to classes."
 
 Is this something that will be rectified in the future?
Recall that a VTable is a seqence of function pointers. For each possible outside instantiation of an in-class template containing virtuals, it would add some more functions. The problem is, multiple instantiations from outside would create conflicting class definitions. So it is not possible. What might be possible, is instantiations within a class - but it's probably not so useful. If you inherit from a class template, you clearly get virtuals, and/or statics, whatever you want. I'm not exactly clear whether it interferes with single inheritance (i.e. whether templates can "mix in") - i'm not a template expert...
 I'm I right in saying that you can add members and functions to a struct using
a
 template?
Never tried, but my guess is yes. Otherwise it would be a bug. :>
 If so, is it the case that there will only be one instance of that
 struct?
Definately not. This will just be the same struct. You have to distunguish between template instantiation (which is code geneation), and, if template represents a type, instantiations of that type. -eye
Apr 23 2004
parent reply Colin JN Breame <Colin_member pathlink.com> writes:
In article <c6aos8$2uuk$1 digitaldaemon.com>, Ilya Minkov says...

[snip]
 This doesn't restrict you in any way, 
[snip] Thanks for clearing that up. [snip]
If you inherit from a class template, you clearly get virtuals, and/or 
statics, whatever you want. I'm not exactly clear whether it interferes 
with single inheritance (i.e. whether templates can "mix in") - i'm not 
a template expert...
[snip] I'm still confused. Does this mean that I can have a template as such: template Foo(D) { class Bar { D d; } } Thanks again.
Apr 23 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Colin JN Breame wrote:

In article <c6aos8$2uuk$1 digitaldaemon.com>, Ilya Minkov says...

[snip]
  

This doesn't restrict you in any way, 
    
[snip] Thanks for clearing that up. [snip]
If you inherit from a class template, you clearly get virtuals, and/or 
statics, whatever you want. I'm not exactly clear whether it interferes 
with single inheritance (i.e. whether templates can "mix in") - i'm not 
a template expert...
    
[snip] I'm still confused. Does this mean that I can have a template as such: template Foo(D) { class Bar { D d; } } Thanks again.
The code your presented works so I don't see your question. Note that if you don't care able your namespaces you can also do this: class Bar(D) { D d; } -- -Anderson: http://badmama.com.au/~anderson/
Apr 23 2004
prev sibling next sibling parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
 "Templates cannot be used to add non-static members or functions to classes."

 Is this something that will be rectified in the future?
This is something that is being changed for the DTL.
Apr 24 2004
prev sibling parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
Colin JN Breame wrote:
 "Templates cannot be used to add non-static members or functions to
 classes."
 
 Is this something that will be rectified in the future?
 
 I'm I right in saying that you can add members and functions to a struct
 using a
 template?  If so, is it the case that there will only be one instance of
 that struct?
If I understand this correctly, it is rather fundamental and cannot be changed in the future. The example given is: class Foo { template TBar(T) { T xx; // Error int func(T) { ... } // Error static T yy; // Ok static int func(T t, int y) { ... } // Ok } } now, if you could do something like Foo x = new Foo(); int a = x.TBar!(int).xx; you would have accessed the non-static member xx. But how many such members does the class actually have? What would the memory layout of an object be? For every instantiation of TBar you would add new members to the class, so the internal memory layout would depend on the order of instantiations, which is something the compiler cannot read from the definition of the class. Same thing for non-static member functions that get entries in the VMT. For static members, there is no problem, since they do not affect the interal memory layout of the class. The compiler just has to allocate memory for global variables, but that is not a problem.
Apr 24 2004