Getting symbol of current function?

John Chapman johnch_atms at hotmail.com
Fri Dec 7 11:27:22 UTC 2018


On Friday, 7 December 2018 at 10:51:38 UTC, Tomer Filiba wrote:
> Is there some clever way to get the *symbol* of the current 
> function, given overloads? I want to log the current function's 
> arguments, but __FUNCTION__ et al only give me a name, which 
> resolves to the first overload. For instance,
>
>     void f(int x) {
>         pragma(msg, "overload1", 
> Parameters!(mixin(__FUNCTION__)));
>     }
>     void f(int x, int y) {
>         pragma(msg, "overload2", 
> Parameters!(mixin(__FUNCTION__)));
>     }
>
> shows
>     overload1(int)
>     overload2(int)
>
> It would be awesome to have something like __FUNCTION_SYM__ or 
> so, which references the actual symbol.
>
> -tomer

This works:

void f(int x) {
   pragma(msg, "overload1", Parameters!(__traits(parent, x))); // 
prints "overload1(int)"
}

void f(int x, int y) {
   pragma(msg, "overload2", Parameters!(__traits(parent, x))); // 
prints "overload2(int, int)"
}

If you don't want to refer to the parameters, you can add a dummy 
variable inside each of the functions and use __traits(parent, 
dummy) instead.


More information about the Digitalmars-d mailing list