How to get a function name (string) @ compile time
Stanislav Blinov
stanislav.blinov at gmail.com
Sun Dec 9 12:22:45 UTC 2018
On Sunday, 9 December 2018 at 03:29:27 UTC, Andrew Pennebaker
wrote:
> Er, when I try to use either foo.stringof, or
> __trait(identifier, foo), I always get that binding name,
> rather than the original function name, sad panda.
>
> I can only print out the current variable name, but I want to
> print the name of the function declaration, no matter how
> deeply I pass that first function pointer into different calls
> :/
You're confusing function pointer with the function symbol. This
will not give you the function name:
import std.stdio;
void wrap(F)(F f) { writeln(__traits(identifier, f)); }
int foo() { return 0; }
void main() { wrap(&foo); }
But this will:
import std.stdio;
auto wrap(alias f, Args...)(auto ref Args args) {
import std.functional : forward;
writeln("Calling ", __traits(identifier, f));
return f(forward!args);
}
int foo() { return 42; }
size_t bar(string x) { return x.length; }
void main() {
assert(wrap!foo == 42);
assert(wrap!bar("hello") == 5);
}
More information about the Digitalmars-d-learn
mailing list