digitalmars.D.learn - overloading a function taking a void[][]
- Johannes Pfau (26/26) Aug 08 2012 I wanted to add a new overload to digest for the std.hash module (see
- Timon Gehr (13/13) Aug 08 2012 Try this:
- Johannes Pfau (6/20) Aug 08 2012 Clever, but doesn't seem to work. I can't create an instance of it, even
- Timon Gehr (5/25) Aug 08 2012 Well, I have done similar things in the past. The general concept should...
- Johannes Pfau (9/19) Aug 09 2012 I see! I didn't have the cast(void[][]) in there. Usually a function
- Johannes Pfau (5/6) Aug 09 2012 Sorry, seems this isn't true. Not sure where I got that information
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
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
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
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>:Well, I have done similar things in the past. The general concept should work.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/d42a58aaMaybe this helps: http://dpaste.dzfl.pl/ed2e8e5d
Aug 08 2012
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.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.BTW: This is a working version, but it does cause template bloat: http://dpaste.dzfl.pl/d42a58aaMaybe this helps: http://dpaste.dzfl.pl/ed2e8e5d
Aug 09 2012
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