January 11, 2012
On 10/01/2012 19:56, Jacob Carlborg wrote:
<snip>
> A template parameter with a template constraint will accept any callable type. Function
> pointer, delegate, struct/class overloading the call operator and so on.

Moreover, if you want to save the callback for later use, you need to distinguish the cases.

But it really just boils down to:
- if it's a global or static function, wrap it in a delegate
- if it's a type with static opCall, wrap class.opCall in a delegate
- if it's an object with an opCall, just use &obj.opCall

I've just had a look at std.functional.toDelegate and it seems it does this straight off.  But the way it wraps a static function in a delegate is a lot more complicated than what my library does - is this just in order to support non-D linkage?

And I see it has the same limitation of not supporting variadics.

Stewart.
January 11, 2012
On 2012-01-11 02:05, Stewart Gordon wrote:
> On 10/01/2012 19:56, Jacob Carlborg wrote:
> <snip>
>> A template parameter with a template constraint will accept any
>> callable type. Function
>> pointer, delegate, struct/class overloading the call operator and so on.
>
> Indeed, this is done in the C++ STL quite a lot.
>
> The drawback is that templated methods lose their virtuality, because it
> cannot be known in advance on what types the template will be
> instantiated in order to populate the vtable.
>
> FWIW my utility library includes a delegate wrapper:
> http://pr.stewartsplace.org.uk/d/sutil/
>
> (dgwrap works in both D1 and D2, though other bits of the library need
> updating to current D2)
>
> Stewart.

Yeah, it all depends on what the needs are.

-- 
/Jacob Carlborg
January 11, 2012
On 2012-01-11 02:21, Stewart Gordon wrote:
> On 10/01/2012 19:56, Jacob Carlborg wrote:
> <snip>
>> A template parameter with a template constraint will accept any
>> callable type. Function
>> pointer, delegate, struct/class overloading the call operator and so on.
>
> Moreover, if you want to save the callback for later use, you need to
> distinguish the cases.
>
> But it really just boils down to:
> - if it's a global or static function, wrap it in a delegate
> - if it's a type with static opCall, wrap class.opCall in a delegate
> - if it's an object with an opCall, just use &obj.opCall
>
> I've just had a look at std.functional.toDelegate and it seems it does
> this straight off. But the way it wraps a static function in a delegate
> is a lot more complicated than what my library does - is this just in
> order to support non-D linkage?
>
> And I see it has the same limitation of not supporting variadics.
>
> Stewart.

I've been doing the same thing myself.

-- 
/Jacob Carlborg
1 2
Next ›   Last »