D:函数参数名内省

发布于 2024-09-30 20:09:36 字数 254 浏览 0 评论 0原文

给定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所谓啦 2024-10-07 20:09:36

您可以使用 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.

难如初 2024-10-07 20:09:36

我认为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文