Q: What are the rules for emitting template code?

Austin Hastings ah08010-d at yahoo.com
Mon Oct 25 06:08:24 PDT 2010


On 10/24/2010 5:16 AM, Simen kjaeraas wrote:
> Austin Hastings <ah08010-d at yahoo.com> wrote:
>
>> If Decider uses Option1.sizeof, does any Option1 code get emitted?
>
> For sizeof to be known, the template would have to be instantiated, yes.

Instantiated does not necessarily imply emitting code, I think. (Of 
course, this is why I asked - to find out the compiler behavior and 
expectations.)

>> If Decider uses a static method of Option1, does any more of the
>> Option1 code get emitted?
>
> As with the first two, it would have to be instantiated.
>
>
> Now, I have the feeling this is not really what you're asking for. What
> do you mean by 'code gets emitted'? Perhaps you could give a reduced
> example?
>


What I am trying to do is to pass a set of expected behavior to a 
template. The template would make a decision based on memory versus 
performance as to which of several possible implementations to use.

For example, a list might be implemented as a dynamic array, or as a 
linked list (or ... or... or...).

If I asked for a list with single-ended insert/remove, then a dynamic 
array is going to win on overhead. If I ask for insert-in-the-middle, 
then the linked list would win on performance.

Ideally, each of the option templates would somehow provide a set of 
metrics so I can ask "hey, for 100 elements, what are your 
memory/performance metrics?"

Which leads to wondering how much of the metric has to be outside the 
option template to avoid inadvertently generating extra object code that 
won't get used.

=Austin



More information about the Digitalmars-d mailing list