C++ Library - <Typeindex>



Introduction

It defines in header and a specialization of hash for this type.

Declaration

Following is the declaration for std::type_index.

class type_index;

C++11

class type_index;

Member functions

Sr.No. Member function & description
1 (constructor)

It is a construct type_index.

2 hash_code

It gets type hash code.

3 name

It gets type name.

4 (relational operators)

It contains type_index operators.

Example

In below example for std::type_index.

#include <iostream>
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
#include <string>

struct C {};

int main() {
   std::unordered_map<std::type_index,std::string> mytypes;

   mytypes[typeid(int)]="It is an integer type";
   mytypes[typeid(double)]="It is a floating-point type";
   mytypes[typeid(C)]="It is a custom class named C";

   std::cout << "int: " << mytypes[typeid(int)] << '\n';
   std::cout << "double: " << mytypes[typeid(double)] << '\n';
   std::cout << "C: " << mytypes[typeid(C)] << '\n';

   return 0;
}

The output should be like this −

int: It is an integer type
double: It is a floating-point type
C: It is a custom class named C
Advertisements