The C++ runtime retrieves the type_info class and bad_typeid exceptions for type information

  • 2020-05-07 20:10:09
  • OfStack

type_info class
The
type_info class describes the type information the compiler generates in the program. An object of this class effectively stores a pointer to the name of the type. The type_info class can also store encoded values suitable for comparing the equality of two types or their order. The encoding rules and ordering of types are unspecified and may vary from program to program.
Must contain < typeinfo > The header file can use the type_info class. The interface of type_info class is:


class type_info {
public:
  virtual ~type_info();
  size_t hash_code() const
  _CRTIMP_PURE bool operator==(const type_info& rhs) const;
  _CRTIMP_PURE bool operator!=(const type_info& rhs) const;
  _CRTIMP_PURE int before(const type_info& rhs) const;
  _CRTIMP_PURE const char* name() const;
  _CRTIMP_PURE const char* raw_name() const;
};

You cannot directly instantiate an object of the type_info class because the class has only one private copy constructor. The only way to construct (temporarily) an type_info object is to use the typeid operator. Because the assignment operator is also private, you cannot copy or assign an object of class type_info.
type_info::hash_code defines a hash function suitable for mapping values of type typeinfo to the distribution of index values.
Operator == and! = is used to compare equality and inequality with other type_info objects, respectively.
There is no correlation between the ordering of types and the inheritance relationship. The sort order of the type can be determined using the type_info::before member function. There is no guarantee that type_info::before will produce the same results in different programs (even if the same program is run multiple times). Thus, type_info::before is similar to the address-of (&) operator.
The type_info::name member function returns const char* to a string ending in null, which represents the user-readable name of the type. The memory to which the cache is directed should never be freed directly.
The type_info::raw_name member function returns const char* to a string ending in null, which represents the modifier name of the object type. The name is actually stored in its decorated form to save space. Therefore, this function is faster than type_info::name because it does not need to undecorate the name. The string returned by the type_info::raw_name function is useful in the comparison operator, but it is not readable. If you need user-readable strings, use the type_info::name function instead.

Abnormal bad_typeid
when the operand of typeid is a pointer to Null, the typeid operator throws an bad_typeid exception.
grammar


  catch (bad_typeid)
statement

note
The interface of bad_typeid is:


class bad_typeid : public exception
{
public:
  bad_typeid(const char * _Message = "bad typeid");
  bad_typeid(const bad_typeid &);
  virtual ~bad_typeid();
};

The following example demonstrates the typeid operator that throws the bad_typeid exception.


// expre_bad_typeid.cpp
// compile with: /EHsc /GR
#include <typeinfo.h>
#include <iostream>

class A{
public:
  // object for class needs vtable
  // for RTTI
  virtual ~A();
};

using namespace std;
int main() {
A* a = NULL;

try {
  cout << typeid(*a).name() << endl; // Error condition
  }
catch (bad_typeid){
  cout << "Object is NULL" << endl;
  }
}

The output


Object is NULL


Related articles: