Understanding Safety of Function Pointers vs. Addresses of Functions

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 15 14:47:28 PDT 2015


On Wednesday, 15 July 2015 at 11:45:00 UTC, Laeeth Isharc wrote:
> Now - is there a way to rewrite my code without mixins?

Not sure that is possible. It would be interesting if someone 
could figure it out though.

I'm more focused on making the "givemeabettername" a bit more 
general. Someone above had sort of asked why bother for the 
simple case. True enough, but if I can write something generic 
enough to work on a wide variety of function types, then I would 
consider it a win. E.g., below.

template givemeabettername(alias fun)
{
	static if (arity!fun == 1)
	{
		T givemeabettername(T)(T x)
			if (isDynamicArray!(T))
		{
			return x.map!fun.array;
		}
		
		T givemeabettername(T)(T x)
			if (isStaticArray!(T))
		{
			T result = x.dup;
			foreach(ref elem; result)
			{
				elem = fun(elem);
			}
			return result;
		}
	}
}


More information about the Digitalmars-d-learn mailing list