Get the name of a function and the parameters?

Jarrett Billingsley jarrett.billingsley at gmail.com
Sun Apr 26 17:13:28 PDT 2009


On Sun, Apr 26, 2009 at 12:23 PM, Jacob Carlborg <doob at me.com> wrote:
> Is it possible to get the name of a function and the names of the function
> parameters?

Name of a function?  Yes.

public template NameOfFunc(alias f)
{
	version(LDC)
		const char[] NameOfFunc = (&f).stringof[1 .. $];
	else
		const char[] NameOfFunc = (&f).stringof[2 .. $];
}

debug
{
	private void _foo_(){}
	static assert(NameOfFunc!(_foo_) == "_foo_", "Oh noes, NameOfFunc
needs to be updated.");
}

It has to be used at compile time with an alias of course, but it works.

Name of params?  No, not that I've found.  Sometimes the compiler will
give parameter names of functions declared with tuple parameters in
error messages; I wonder if that could be abused.

Don't you love it?  "Most C++ template features are discovered."  So are D's.


More information about the Digitalmars-d-learn mailing list