Problem with templates
BCS
ao at pathlink.com
Mon Jul 7 11:11:11 PDT 2008
Reply to Sean,
> BCS Wrote:
>
>> Reply to Sean,
>>
>>>> It can't. Delegates and functions currently have different calling
>>>> conventions and the compiler cannot automatically convert one to
>>>> the other. Was this just intuition or did you read that it would
>>>> somewhere?
>>>>
>>> Take this example:
>>>
>> [...]
>>
>>> void main()
>>> {
>>> int plus(int x, int y, int z)
>>> {
>>> return x + y + z;
>>> }
>>> auto plus_two = Curry(&plus, 2);
>>> Notice how the function Curry accepts a delegate, but a function
>>> pointer is actually passed in. I have personally re-written this
>>> function to take advantage of D2 closures and it worked perfectly
>>> fine.
>>>
>> taking the address of a (non static) nested functions generates a
>> delegate, not a function pointer.
>>
> I see. Any idea why this revised function still doesn't work?
>
> R delegate(T) my_compose(R, IR, IT, T...)(IR function(T) first, R
> function(IT) second) {
> return delegate(T args) { return second(first(args)); };
> }
> test.d(40): template test.my_compose(R,IR,IT,T...) does not match any
> function template declaration
>
> test.d(40): template test.my_compose(R,IR,IT,T...) cannot deduce
> template function from argument types !()(shortC function(short,
> void*, void**),void function(short rc))
>
> test.d(40): Error: function expected before (), not
> (my_compose(R,IR,IT,T...))((& SQLAllocHandle),(& SQL)) of type int
>
I don't have time right now to dig out the context but... IFTI might just
not be up to it.
you might try something like this
ReturnTypeOf!(S) delegate(ArgsOf!(F)) my_compose(F, S)(F first, S second)
{
static assert(IsAFunctionType!(F)); // this might do better as a constraint
static assert(IsAFunctionType!(S));
return ReturnTypeOf!(S) delegate(ArgsOf!(F) args) { return second(first(args));
};
}
I'm sure you can find or write the used templates.
http://www.digitalmars.com/d/2.0/phobos/std_traits.html
More information about the Digitalmars-d-learn
mailing list