Get the name of a function and the parameters?

Robert Fraser fraserofthenight at gmail.com
Sun Apr 26 23:01:37 PDT 2009


Jarrett Billingsley wrote:
> 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.

You can get the name of a local variable from within a scope that 
variable is active at compile-time. Pass it as an alias parameter and 
then start demangling in there; it's usually towards the end.


More information about the Digitalmars-d-learn mailing list