Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::type_index

      From cppreference.com
      <cpp‎ |types
       
       
      Utilities library
       
       
       
      Defined in header<typeindex>
      class type_index;
      (since C++11)

      Thetype_index class is a wrapper class around astd::type_info object, that can be used as index in associative and unordered associative containers. The relationship withtype_info object is maintained through a pointer, thereforetype_index isCopyConstructible andCopyAssignable.

      Contents

      [edit]Member functions

      constructs the object
      (public member function)[edit]
      (destructor)
      (implicitly declared)
      destroys thetype_index object
      (public member function)
      operator=
      (implicitly declared)
      assigns atype_index object
      (public member function)
      compares the underlyingstd::type_index objects
      (public member function)[edit]
      returns hashed code
      (public member function)[edit]
      returns implementation defined name of the type,
      associated with underlyingtype_info object
      (public member function)[edit]

      [edit]Helper classes

      hash support forstd::type_index
      (class template specialization)[edit]

      [edit]Example

      The following program is an example of an efficient type-value mapping.

      Run this code
      #include <iostream>#include <memory>#include <string>#include <typeindex>#include <typeinfo>#include <unordered_map> struct A{virtual ~A(){}}; struct B: A{};struct C: A{}; int main(){std::unordered_map<std::type_index,std::string> type_names;     type_names[std::type_index(typeid(int))]="int";    type_names[std::type_index(typeid(double))]="double";    type_names[std::type_index(typeid(A))]="A";    type_names[std::type_index(typeid(B))]="B";    type_names[std::type_index(typeid(C))]="C"; int i;double d;    A a; // note that we're storing pointer to type Astd::unique_ptr<A> b(new B);std::unique_ptr<A> c(new C); std::cout<<"i is "<< type_names[std::type_index(typeid(i))]<<'\n';std::cout<<"d is "<< type_names[std::type_index(typeid(d))]<<'\n';std::cout<<"a is "<< type_names[std::type_index(typeid(a))]<<'\n';std::cout<<"*b is "<< type_names[std::type_index(typeid(*b))]<<'\n';std::cout<<"*c is "<< type_names[std::type_index(typeid(*c))]<<'\n';}

      Output:

      i is intd is doublea is A*b is B*c is C

      [edit]See also

      contains some type’s information, the class returned by the typeid operator
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/type_index&oldid=167945"

      [8]ページ先頭

      ©2009-2025 Movatter.jp