Why can't templates use tuples for for argument types?

Bill Baxter dnewsgroup at billbaxter.com
Thu Jul 19 12:10:38 PDT 2007


BCS wrote:
> Reply to Bill,
> 
> 
>>
>> I don't really understand the point of wanting to be able to specify a
>> template argument that could be *anything* -- alias, value, or symbol.
>> Other than a big static if, I can't see how you would be able to
>> implement any functionality when it could be any one of those things.
>>
>> But I'm probably just being dense.
>>
>> --bb
>>
> 
> One thing that it would be useful for would be a tuple template that 
> does something with any tuple type
> 
> // take exactly 2 and return true if they are the same
> template IsSame(A..,B..){const bool IsSame = ...}
> 
> // take 1 or more and remove all of the first from the rest
> template Remove(remove.., from...)
> {
>  static if(from.length == 0)
>  {
>     alias from Remove;
>  }
>  else
>  {
>    staic if(IsSame!(remove, from[0]))
>    {
>       alias Remove!(remove,from[1..$]) Remove
>    }
>    else
>    {
>       alias T!(from[0], Remove!(remove,from[1..$])) Remove
>    }
>  }
> }
> 
> this would work as:
> 
> Remove!(1,4,5,6,7,1,2,3); // gives (4,5,6,7,2,3)
> Remove!(int, byte, short, int, long); // gives (byte, short, long)

Ok.  So it's basically for writing functions that manipulate tuples 
themselves.  I agree, it does make sense to be able to do that, in a 
Lisp-y completeness kind of way.

You can write something like Remove!(1).from!(4,5,6,1,2,3,4) now.  Then 
if the first tuple is empty you just don't remove anything (and you can 
additionally enhance it to remove any of N things from the list instead 
of just 1 thing).  But still, tuples are the cons and cdr of template 
metaprogramming.  There shouldn't be arbitrary restrictions on how you 
can manipulate them based on what's in the slots.

As an aside, I think Walter's right about the dots.  I misread your 
example as being two '..'s initially instead of one .. and one ... arg. 
  I did just wake up though. :-)

--bb


More information about the Digitalmars-d-learn mailing list