Code example for function/delegate as template argument ?
Basile B. via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue May 3 23:59:00 PDT 2016
On Wednesday, 4 May 2016 at 06:21:36 UTC, chmike wrote:
> Hello,
>
> I failed to find some code example for a template class/struct
> that accept a function/delegate as template argument. All
> examples I could find use simple value types like int or double.
>
>
> I piggy bag another question. Defining a function/delegate as
> function argument is shown in examples. What I could not find
> is how would I pass an object instance with a method to call ?
> In C++ we use std::bind. How do we do that in D ?
Hello, you can use an alias this to pass a lmbda (or a delegate):
----
module runnable;
import std.stdio;
struct Foo(alias fun)
{
this(string text)
{
fun(text);
}
}
void main(string[] args)
{
alias fun = (a) => a.writeln;
auto foo = Foo!fun("hello");
}
----
I suppose that a constraint would be welcome be that's not the
point here.
More information about the Digitalmars-d-learn
mailing list