Array of member functions generated at compile time

Jason Langenauer jason at jasonlangenauer.com
Fri Apr 25 06:34:41 PDT 2008


Hi,

I'm trying to generate at compile-time an associative array of
function pointers to class member functions where the key in the array
is the name of the member function itself.

What my best attempt so far has been is as follows, which doesn't compile:

class ApplicationController
{
  void function()[char[]] action_map;

  this()
  {
    mixin ArrayMap!(build_code(__traits( derivedMembers,
ApplicationController )));
  }

  char[] build_code(invariant(char)[][] actions)
  {
    char[] code;
    foreach(invariant(char)[] action; actions)
      {
        code ~= "action_map[\"" ~ action ~ "\"] = &(" ~ action ~ ");";
      }
    return code;
  }

  template ArrayMap(char[] code)
  {
    const ArrayMap = code;
  }
}

Can anyone suggest other ways to achieve what I want to do? Or point
out where I might be going wrong in the above?

Many thanks

Jason

P.S. The compiler error I get is:
applicationcontroller.d(9): Error: expression
this.build_code(["action_map","_ctor","build_code","ArrayMap"]) is not
a valid template value argument


More information about the Digitalmars-d-learn mailing list