C ++是否已经有了某种反映? [英] Does C++ already have some kind of reflection?

查看:93
本文介绍了C ++是否已经有了某种反映?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此示例:

struct Nobody_Expects_The_Spanish_Inquisition{};

int main(){
    throw Nobody_Expects_The_Spanish_Inquisition();
}

Idone


在抛出 Nobody_Expects_The_Spanish_Inquisition 实例后终止调用>'

Windows的类似输出:

Similar output for Windows:


Test.exe中0x760fb727处未处理的异常:Microsoft C ++异常:内存位置0x001ffea3处的Nobody_Expects_The_Spanish_Inquisition ..

可以看出,最终程序集似乎已经包含了异常的名称,或者还有另一种获取名称的方法。

As can be seen, the final assembly seems to contain the name of the exception already, or there is another method to acquire the name.

这可以看作是某种反思?还是实际上可以显示异常的名称,这是否取决于编译器/操作系统?

Can this be seen as some kind of reflection? Or is it compiler/OS dependent if the name of the exception can actually be displayed?

推荐答案

取决于编译器。显然,编译器很容易发现每个抛出,并将每个抛出对象的类型编码为可执行文件。

It's compiler dependant. Obviously, it's easy for the compiler to spot every throw, and encode the type of every thrown object into the executable. But there is no requirement that they should do this.

考虑一下,抛出异常时必须将异常复制到与实现相关的怪异空间中。因此,可以通过这种机制访问特定编译器的类型名称。

And thinking about it, exceptions have to be copied into a weird implementation-dependent space when they are thrown. So it makes sense that the name of their type is accessible via this mechanism to the runtime of a specific compiler.

这篇关于C ++是否已经有了某种反映?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆