Denis Gladkiy
Denis Gladkiy

Reputation: 2174

How to list template methods of a class in D?

__traits(allMembers, Clazz) returns all the class Clazz members. I can find all methods using "MemberFunctionsTuple" function. But how can I get only the template methods?

Upvotes: 2

Views: 54

Answers (1)

rcorre
rcorre

Reputation: 7218

It seems (through very cursory testing), that typeof(T.member) will return void for a template and non-void for a field or non-templated function (a regular function returning void would be void(), not void). Taking advantage of this:

import std.traits, std.meta;

class C {
  enum e = 5;         // compile-time value
  int field;          // plain field, not a template
  void fun() { }      // plain function, not a template
  void tfun()() { }   // templated function
  template temp() { } // template

  void both(int i) { }
  void both(T : string)(T i) { }
}

template allTemplateMembers(T) {
  enum isTemplateMember(string name) = is(typeof(mixin("T."~name)) == void);

  alias allTemplateMembers = Filter!(isTemplateMember, __traits(allMembers, T));
}

pragma(msg, allTemplateMembers!C); // tuple(tfun, temp)

Note that both does not pass, as it has both a templated and non-templated overload. At the moment I'm not sure how to modify allTemplateMembers if you did want such a member to pass.

Upvotes: 2

Related Questions