C++ Type_info Library - name Function



Description

It returns a null-terminated character sequence that may identify the type.

Declaration

Following is the declaration for std::type_info::name.

C++98

	
const char* name() const;

C++11

const char* name() const noexcept;

Parameters

none

Return Value

It returns a null-terminated character sequence that may identify the type.

Exceptions

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

Data races

The locale object is modified.

Example

In below example for std::type_info::name.

#include <iostream>
#include <typeinfo>

struct Base { virtual ~Base() = default; };
struct Derived : Base {};

int main() {
   Base b1;
   Derived d1;

   const Base *pb = &b1;
   std::cout << typeid(*pb).name() << '\n';
   pb = &d1;
   std::cout << typeid(*pb).name() << '\n';

   return 0;
}

The output should be like this −

4Base
7Derived
typeinfo.htm
Advertisements