Template pattern delegate?
Jacob Carlborg
doob at me.com
Mon Oct 26 09:25:03 UTC 2020
On Monday, 26 October 2020 at 00:56:26 UTC, frame wrote:
> I see that your approach can handle functions and delegates but
> isn't that not equivalent like this template for a function?
>
> auto myStuff(T)(T function() fn) {
> try {
> return fn();
> }
> catch (Exception e) {
> //
> }
> }
You cannot pass a delegate to something that expects a function
pointer.
> I wonder if I could use such a template as static variant and
> the compiler just expands the code? Just thinking that using a
> function or delegate is an overhead. Maybe not a function but a
> delegate will allocate GC memory I think.
If you pass the delegate as a template parameter/alias parameter,
it's more likely to be inlined:
auto myStuff(alias fn)() {
try return fn();
catch (Exception e) { }
}
myStuff!( { /* some code */ } );
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list