This program:
#include <typeinfo>
#include <iostream>
struct Nobody_Expects_The_Spanish_Inquisition {
};
namespace junk {
struct I_didnt_expect_a_kind_of_spanish_inquisition
: public Nobody_Expects_The_Spanish_Inquisition
{
};
}
int main(int argc, const char *argv[])
{
using ::std::type_info;
using ::std::cout;
Nobody_Expects_The_Spanish_Inquisition foo;
junk::I_didnt_expect_a_kind_of_spanish_inquisition bar;
const type_info &fooinfo = typeid(foo);
const type_info &barinfo = typeid(bar);
cout << "The type of foo is <" << fooinfo.name() << ">\n";
cout << "The type of bar is <" << barinfo.name() << ">\n";
return 0;
}
has this output:
$ ./foo
The type of foo is <38Nobody_Expects_The_Spanish_Inquisition>
The type of bar is <N4junk44I_didnt_expect_a_kind_of_spanish_inquisitionE>
This is as good as it gets for introspection in C++. And this is just barely enough to accomplish what the default terminate handler is doing.
While, as others have pointed out, the default terminate handler is allowed to accomplish this goal anyway it darn well pleases, I'd be surprised if it didn't make use of the same mechanisms that are used to implement typeid
to make this work.
Of course, the default terminate handler could work by accessing a special area the compiler creates whenever an exception is thrown that records whatever the compiler knows about the name of the type at the spot where it's thrown. As other's have pointed out, the default terminate handler is put there by the compiler and is not bound by any of the rules code written by a C++ programmer would have to follow.
I've seen people write their own terminate handlers that hand-walk the call stack and lookup debug symbols associated with each address to get some facsimile of a stack-trace. These are compiler and platform specific magic, and since the compiler knows exactly which compiler it is and what platform its being used on, it could have a default terminate handler that did the same thing on supported platform.
In summary, RTTI is not required to implement the feature you're noting. But RTTI is a very rudimentary form of reflection, and could be used to implement that feature.