Introspection/Reflection/etc on Linux

Robert Jacques sandford at jhu.edu
Sun Oct 23 09:21:37 PDT 2011


On Sun, 23 Oct 2011 02:59:48 -0400, J Arrizza <cppgent0 at gmail.com> wrote:

> The idea here was to create a base class. That base class would have a
> function register() which took a function name or function pointer, and
> create a delegate at compile time. register() would add the delegate  and
> the function name as a string into the hash. At runtime, I pass in the
> string, get the delegate and invoke the function.
>
> So I need both the function name as a symbol (to create the delegate) and
> the function name as a string. In c++ I'd use the preprocessor to convert
> the symbol to a string via "#" (stringify).
>
> If D doesn't have this, the callers will have to do:
>
>      register(bob, "bob");
>
> which is a little tedious.
>
> BTW I tried:
>
>    public void register(string name)   {
>      auto func = __traits(getMember, this, name);
>
> but __traits() only works at compile time, but variable name is only
> available at run-time....
>
> John

Okay, I'm a little confused about exactly what you want to do, but I think you want something like:

string foo(alias T)() {
     T++;
     return T.stringof;
}

int x = 0;
assert(foo!x == "x");
assert(x == 1);

alias template parameters give you access to actual variables/functions, which is what you seem to be asking for.


More information about the Digitalmars-d mailing list