digitalmars.D.learn - Overloading funtion templates.
- Balagopal Komarath (24/24) Jun 28 2017 Shouldn't the compiler be able to resolve foo!g(3) to the first
- vit (7/31) Jun 28 2017 symbol 'g' isn't type.
- Balagopal Komarath (3/7) Jun 28 2017 With this definition foo!((x) => x+1)(3); doesn't work. Is there
- vit (24/31) Jun 29 2017 You don´t need overload templates:
- Balagopal Komarath (2/8) Jun 29 2017 Thanks. That works.
Shouldn't the compiler be able to resolve foo!g(3) to the first template foo? import std.stdio; import std.algorithm; import std.range; auto foo(F, T)(T x) { return x.foo(F); } auto foo(F, T)(T x, F f) { return f(x); } int g(int x) { return x; } void main() { foo(3, &g); // 2nd foo foo!g(3); // error } I get the error message. onlineapp.d(20): Error: template onlineapp.foo cannot deduce function from argument types !(g)(int), candidates are: onlineapp.d(5): onlineapp.foo(F, T)(T x) onlineapp.d(10): onlineapp.foo(F, T)(T x, F f)
Jun 28 2017
On Wednesday, 28 June 2017 at 11:49:57 UTC, Balagopal Komarath wrote:Shouldn't the compiler be able to resolve foo!g(3) to the first template foo? import std.stdio; import std.algorithm; import std.range; auto foo(F, T)(T x) { return x.foo(F); } auto foo(F, T)(T x, F f) { return f(x); } int g(int x) { return x; } void main() { foo(3, &g); // 2nd foo foo!g(3); // error } I get the error message. onlineapp.d(20): Error: template onlineapp.foo cannot deduce function from argument types !(g)(int), candidates are: onlineapp.d(5): onlineapp.foo(F, T)(T x) onlineapp.d(10): onlineapp.foo(F, T)(T x, F f)symbol 'g' isn't type. auto foo(alias F, T)(T x) { return x.foo(&F); }
Jun 28 2017
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?
Jun 28 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: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); }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?
Jun 29 2017
On Friday, 30 June 2017 at 04:51:23 UTC, vit wrote:import std.traits : isCallable; auto foo(alias F, T)(T x) if(isCallable!F) //this line is optional { return F(x); }Thanks. That works.
Jun 29 2017