February 09, 2005
This may be a bit off topic, but since we're talking templates and flexibility and expandability, would variadic templates be worth anything?  I was playing around with a function that had a template for it and I converted the function to be a variadic function.  Then as I was converting the template, I realized I couldn't do it.  The more I thought about it, the more a variadic template just seemed to me like a variadic function.  But I just still couldn't come to think of the two as the same and I couldn't fully get my head around trying to resolve what felt like differences between them (even though I couldn't think of any, or come up with an expressive example).  These may just be wasted thought cycles, but I also thought why not?


In article <cu9ruo$e4o$1@digitaldaemon.com>, Norbert Nemec says...
>Walter wrote:
>> "Norbert Nemec" <Norbert@Nemec-online.de> wrote in message news:ctvj6q$248a$1@digitaldaemon.com...
>>> The power of C++ implicit instantiation comes from very sopisticated
>> pattern
>>> matching: The compiler can match any type like
>>>  MyTemplate<int,float,MyTemplate2<5,int> >
>>> with a type pattern like
>>>  MyTemplate<T,U,MyTemplate2<N,T> >
>>> while also matching up T with int, U with float and N with 5
>> So can D. D will do all the pattern matching C++ will, it just won't do implicit instantiation. The above examples are explicit instantiation, which should work fine with D.
>I guess, I did not make my point clear enough: as I believe, the pattern matching in C++ is fundamentally more powerful that that in D. To illustrate, it is the simplest way to imagine what implicit instantiation might look like in D without demanding more intelligence of the compiler.
>The first idea that comes to mind would be the rule that, if 'mytemplate' is the name of a template containing exactly one function with the name 'mytemplate', the call
> mytemplate(a,b,c)
>is automatically translated into
> mytemplate!(typeof(a),typeof(b),typeof(c))(a,b,c)
>This was my first idea when I thought about the topic. It would be trivial to implement and provide some kind of 'implicit instantiation'. It can make full use of D's pattern matching mechanism, as it will automatically select the appropriate specialization based on the types of the arguments.
>However, it becomes immediately clear that this is less powerful than C++. The template parameters could only be types and they would have to match one-by-one with the arguments of the encapsulated function. C++, on the other hand allows the template parameters to be used as placeholders in arbitrary places of the function arguments:
> template<int M,int N>
> Vector<M> dot(Matrix<M,N> a,Vector<N> b)
> {
>  Vector<M> res;
>  for(int m=M;m--;) {
>      double r = 0.0;
>      for(int n=N;n--;)
>   r += a[m,n]*b[n]
>      res[m] = r;
>  }
>  return res;
> }
>Here, the compiler actually does a complete patter matching on the pair
> (Matrix<$M,$N>,Vector<$N>)
>and keeps the integers matched by $M and $N for later use.
>Furthermore, the compiler matches not only one pattern, but compares several patterns and selects the one that fits "best" (if you do partial specialization)
>This goes several steps beyond the current power of D.

February 09, 2005
Kramer wrote:

> This may be a bit off topic, but since we're talking templates and flexibility and expandability, would variadic templates be worth anything?

Similar things have been discussed for C++ as well. I don't know whether complete concepts arose from it. Basically, it would be very helpful to have them. There is a fundamental difference between variadic templates and variadic function, in so far that the former are resolved at compile time and the latter at runtime. Maybe this could also be a way to have compile-time checked variadic functions that have been proposed on this list before.

Next ›   Last »
1 2 3
Top | Discussion index | About this forum | D home