The definition of templates in D

FeepingCreature default_357-line at yahoo.de
Sun Mar 18 09:05:46 PDT 2012


On 03/18/12 12:05, Derek wrote:
> On Sun, 18 Mar 2012 21:36:46 +1100, FeepingCreature <default_357-line at yahoo.de> wrote:
> 
> 
>> why would you do that
> 
> To make coding easier to write AND read.
> 
>> what do you want to _do_
> 
> Infer template arguments from the data types presented in the data values supplied on the instantiation statement.
> 
>> it sounds like you're frantically trying to nail templates into a shape that they really really really aren't meant for
> 
> I assumed D templates were a type of template; a model for real runnable code that the compiler can instantiate based on the arguments supplied.

Yes but you keep trying to pass runtime arguments to a compiletime construct.

I think you understand what templates are but not how to use them.

> 
>> in any case what is wrong with auto add(T)(T t) { return t[0] + t[1]; }
> 
> It doesn't work.
> 

Okay, let's try this.

template add(T) {
  template add(U...) {
    T add(U u) {
      T res;
      foreach (entry; u) res += cast(T) entry;
      return res;
    }
  }
}

add!int(2, 3, 4.0, 5.0f);


More information about the Digitalmars-d mailing list