D:函数参数名内省
给定 D 中的函数声明,是否可以在编译时内省任何函数参数名称的字符串表示形式,以用于自动函数反射。 例如,
void foo(int a, double b, string c) { }
register_function!(foo)()
register_function 可以在编译时以 __traits(allMembers,someClass) 为类提取“a”、“b”、“c”的类似方式提取“a”、“b”、“c”吗?
Given a function declaration in D, is it possible to introspect at compile time the string representation of any function parameter names, for use in say automatic function reflection.
E.g.
void foo(int a, double b, string c) { }
register_function!(foo)()
Can register_function extract "a","b","c" at compile time in a similar way that __traits(allMembers,someClass) can for a class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 std.traits.ParameterTypeTuple!() 来获取参数的类型,但我不知道有什么方法可以获取它们的名称。然而,std.traits 正在不断改进,以便添加我的内容。很可能只是没有人考虑到这一特殊需求,所以他们还没有添加它。我建议为其创建一个增强请求,而且很有可能他们会添加它。
You can use
std.traits.ParameterTypeTuple!()
to get the types of the parameters, but I'm not aware of any way to get their names.std.traits
is continuously being improved, however, so that my get added. Odds are that is just that no one working on it has thought of that particular need, so they haven't added it yet. I would suggest creating an enhancement request for it, and there's a good chance that they'll add it.我认为 stringof 的用途之一是给出名称。您可以通过一些工作来解析它们。 OTOH stringof 定义不明确,所以这会有点脆弱。
I think one of the uses of stringof gives the names. You can parse them out with a bit of work. OTOH stringof is ill-defined so this would be a bit brittle.