C++ Type_info Library - bad_typeid



Description

It throws an exception on typeid of null point.

Declaration

Following is the declaration for std::bad_typeid.

C++98

	
class bad_typeid;

C++11

class bad_typeid;

Parameters

none

Return Value

none

Exceptions

No-throw guarantee − this member function never throws exceptions.

Data races

The locale object is modified.

Example

In below example for std::bad_typeid.

#include <iostream>
#include <typeinfo>
 
struct S {
   virtual void f();
};

int main() {
   S* p = nullptr;
   try {
      std::cout << typeid(*p).name() << '\n';
   } catch(const std::bad_typeid& e) {
      std::cout << e.what() << '\n';
   }
}

The output should be like this −

Attempted a typeid of NULL pointer!
typeinfo.htm
Advertisements