Modify Function Pointer to Take Additional Parameters

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 19 07:00:51 PST 2016


On Friday, 19 February 2016 at 14:21:26 UTC, Kagamin wrote:
>
> int bar(int x)
> {
> 	return x;
> }
>
> int baz(int x, int y)
> {
> 	return bar(x);
> }
>
> void main()
> {
> 	import std.stdio : writeln;
>
> 	int function(int x, int y) foo_bar = &baz;
> 	
> 	writeln(foo_bar(1, 2));
> }

This works.

But when I re-write foo to take that into account as in below, I 
get an error that I can't implicitly convert int function(int x) 
to int function(int x, int y).

auto foo(T)(T f)
{
	static if (is(T == fp2))
	{
		return f;
	}
	else static if (is(T == fp1))
	{
		int function(int x, int y) f_ = f;
		return f_;
	}
	else
	{
		return 0;
	}
}


More information about the Digitalmars-d-learn mailing list