Overloading funtion templates.
vit via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 29 21:51:23 PDT 2017
On Thursday, 29 June 2017 at 06:40:04 UTC, Balagopal Komarath
wrote:
> On Wednesday, 28 June 2017 at 12:19:31 UTC, vit wrote:
>> auto foo(alias F, T)(T x)
>> {
>> return x.foo(&F);
>> }
>
> With this definition foo!((x) => x+1)(3); doesn't work. Is
> there a way to solve this?
You don“t need overload templates:
import std.traits : isCallable;
auto foo(alias F, T)(T x)
if(isCallable!F) //this line is optional
{
return F(x);
}
int g(int x) { return x; }
struct G{
int i;
this(int i){
this.i = i;
}
int opCall(int x){return x*i;} //int operator()(int x)
}
void main(){
foo!g(3);
foo!((int x) => x*2)(3);
auto g2 = G(4);
foo!g2(3);
foo!(G(5))(3);
}
More information about the Digitalmars-d-learn
mailing list