C++11 反射库

发布于 2024-10-03 08:17:23 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

白馒头 2024-10-10 08:17:24

好吧,根据您的需求,您可以在 python 和 clang python 绑定之上构建您自己的。有一些例子,例如我自己的 https://github.com/nevion/metapod - 如果您足够努力地进行清理,那么其他工具就存在,并且在某种程度上,这就是 Qt 的 MOC 工具正在做的事情 - 除了 clang 使任务变得更加容易。这种方法的一个很酷的事情是它适用于所有编译器,因为您只是使用 mako 模板生成标准化代码 - 但这也意味着它不是完​​全自动的 - 所以需要权衡,它不会处理每个元编程/反思需要。正如 Charles Salvia 所说,C++ 并不是最好的反思语言,所以你必须接受你能得到的。

Well depending on what you're after, you could build your own ontop of python and clang python bindings. There are a few examples out there, such as my own https://github.com/nevion/metapod - others out there exist if you scavenge hard enough and in a way this is what Qt's MOC tool is doing - except clang makes the task so much easier. One of the cool things about this approach is it works for all compilers because you're just generating standardized code with mako templates - but this also means it is not fully automatic - so there's a trade-off and it wont deal with every metaprogramming/reflection need. As Charles Salvia said, C++ isn't the best language for reflection so you have to take what you can get.

爱她像谁 2024-10-10 08:17:23

C++ 并不是真正最好的反射语言。 C++0x 并没有真正改变这一点。您可以使用类型特征获得对静态反射的有限支持,甚至可以使用 SFINAE 静态确定某个类是否具有特定的成员函数或成员变量。但确实如此。

动态反射受到严重限制。您可以使用 工具在运行时获取类的类型,但仅此而已。

至于静态反射,在没有严重妥协的情况下,一般迭代类并获取每个成员变量/函数的能力是不可能的。 Boost.Fusion 通过提供宏来成功实现这一点,这些宏允许您将对象绑定到类似元组的容器。事实上,std::tuple 或 boost::tuple 类自然地提供编译时反射 - 换句话说,您可以静态迭代元组并确定类型每个成员的。这为您提供了对任意聚合类型的近似编译时反射。不幸的是,它不像为任意类内置本机反射支持那么方便。

C++ is not really the best language for reflection. C++0x doesn't really change that. You can get limited support for static reflection using type traits, and you can even use SFINAE to statically determine whether a certain class has a particular member function or member variable. But that's really it.

Dynamic reflection is severely limited. You can get the type of a class at runtime using the <typeinfo> facilities, but that's about it.

As for static reflection, the ability to generically iterate over a class and get each member variable/function is just not possible without serious compromises. Boost.Fusion manages to pull this off by providing macros which allow you to bind an object to a tuple-like container. In fact, the std::tuple or boost::tuple class naturally provide compile-time reflection - in other words, you can statically iterate over a tuple and determine the type of each member. This gives you something approximating compile-time reflection over arbitrary aggregate types. Unfortunately, it's not as convenient as if there were native reflection support built in for arbitrary classes.

靖瑶 2024-10-10 08:17:23

似乎有一个库可以满足您的“广泛”要求。看一下镜像:Boost.Mirror。请注意,它正式不是 boost 的一部分。您还可以查看其他库:反射编程

It seems that there is a library that satisifies your "broad" requirements. Take a look at Mirror: Boost.Mirror. Note that it is officially NOT part of boost. You also could take a look at other libraries: Reflective Programming.

披肩女神 2024-10-10 08:17:23

QT 有一种原始的反射形式,您可能想利用它。

QT has a primitive form of reflection, you probably want to take a loot at it.

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