问题描述
考虑此示例:
struct Nobody_Expects_The_Spanish_Inquisition{};
int main(){
throw Nobody_Expects_The_Spanish_Inquisition();
}
在:
Windows的类似输出:
Similar output for Windows:
可以看出,最终程序集似乎已经包含了异常的名称,或者还有另一种获取名称的方法。
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 ++是否已经有了某种反映?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!