www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - overloading a function taking a void[][]

reply Johannes Pfau <nospam example.com> writes:
I wanted to add a new overload to digest for the std.hash module (see
review in main newsgroup). This is the code written as two functions
which should be combined in one overloaded function

----------------
digestType!Hash digest(Hash)(scope const(void[])[] data...)
    if(isDigest!Hash)
{
    //implementation detail
}

digestType!Hash digestRange(Hash, Range)(Range data) if(isDigest!Hash &&
    isInputRange!Range && __traits(compiles,
    digest!Hash(ElementType!(Range).init)))
{
    //implementation detail
}
----------------

What I need is two overloads: One which takes a byte representation of
every possible type (and multiple parameters of that), except
InputRanges. And one which handles the InputRanges.
As an additional difficulty the overload taking all other types should
only cause one template instance (per Hash type) to avoid template
bloat (like currently).

It must also be possible to alias the template for specific Hash types
(could be done with a template instead of an alias as long as it doesn't
cause bloat):

alias digest!MD5 md5Of; //Works right now
Aug 08 2012
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
Try this:

template digest(Hash) if(isDigest!Hash){
	digestType!Hash digest(Range)(Range data) if(!is(Range:void[][]) && 
isInputRange!Range && 
__traits(compiles,digest!Hash(ElementType!(Range).init))){
		//implementation detail
	}
	digestType!Hash digest()(scope const(void[])[] data...){ // templated 
as a workaround
		//implementation detail
	}
         // ... (maybe more overloads)
}
Aug 08 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/08/2012 09:11 PM, Johannes Pfau wrote:
 Am Wed, 08 Aug 2012 18:53:05 +0200
 schrieb Timon Gehr<timon.gehr gmx.ch>:

 Try this:

 template digest(Hash) if(isDigest!Hash){
 	digestType!Hash digest(Range)(Range data)
 if(!is(Range:void[][])&&  isInputRange!Range&&
 __traits(compiles,digest!Hash(ElementType!(Range).init))){
 		//implementation detail
 	}
 	digestType!Hash digest()(scope const(void[])[] data...){ //
 templated as a workaround
 		//implementation detail
 	}
           // ... (maybe more overloads)
 }

Clever, but doesn't seem to work. I can't create an instance of it, even when explicitly specifying the types.

Well, I have done similar things in the past. The general concept should work.
 BTW: This is a working version, but it does cause template bloat:
 http://dpaste.dzfl.pl/d42a58aa

Maybe this helps: http://dpaste.dzfl.pl/ed2e8e5d
Aug 08 2012
prev sibling next sibling parent Johannes Pfau <nospam example.com> writes:
Am Wed, 08 Aug 2012 18:53:05 +0200
schrieb Timon Gehr <timon.gehr gmx.ch>:

 Try this:
 
 template digest(Hash) if(isDigest!Hash){
 	digestType!Hash digest(Range)(Range data)
 if(!is(Range:void[][]) && isInputRange!Range && 
 __traits(compiles,digest!Hash(ElementType!(Range).init))){
 		//implementation detail
 	}
 	digestType!Hash digest()(scope const(void[])[] data...){ //
 templated as a workaround
 		//implementation detail
 	}
          // ... (maybe more overloads)
 }

Clever, but doesn't seem to work. I can't create an instance of it, even when explicitly specifying the types. BTW: This is a working version, but it does cause template bloat: http://dpaste.dzfl.pl/d42a58aa
Aug 08 2012
prev sibling next sibling parent Johannes Pfau <nospam example.com> writes:
Am Wed, 08 Aug 2012 21:55:21 +0200
schrieb Timon Gehr <timon.gehr gmx.ch>:

 
 Well, I have done similar things in the past. The general concept
 should work.
 
 BTW: This is a working version, but it does cause template bloat:
 http://dpaste.dzfl.pl/d42a58aa

Maybe this helps: http://dpaste.dzfl.pl/ed2e8e5d

I see! I didn't have the cast(void[][]) in there. Usually a function defined with (void[][] data...) can take an arbitrary number of arguments of any type. But when used as above, it looses that feature and an explicit cast is necessary. http://dpaste.dzfl.pl/8bb247ff Not sure if I like this, I'll probably just have a separate function digestRange.
Aug 09 2012
prev sibling parent Johannes Pfau <nospam example.com> writes:
Am Thu, 9 Aug 2012 10:20:45 +0200
schrieb Johannes Pfau <nospam example.com>:


 arguments of any type.

Sorry, seems this isn't true. Not sure where I got that information from, void[][]... only works with all array types, not all types in general.
Aug 09 2012