Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::type_info::name

      From cppreference.com
      <cpp‎ |types‎ |type info
       
       
      Utilities library
       
       
       
      constchar* name()const;
      (noexcept since C++11)

      Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      Null-terminated character string containing the name of the type.

      [edit]Notes

      The lifetime of the array pointed to by the returned pointer is not specified, but in practice it persists as long as the RTTI data structure for the given type exists, which has application lifetime unless loaded from a dynamic library (that can be unloaded).

      Some implementations (such as MSVC, IBM, Oracle) produce a human-readable type name. Others, most notably gcc and clang, return the mangled name, which is specified by theItanium C++ ABI. The mangled name can be converted to human-readable form using implementation-specific API such asabi::__cxa_demangle directly or throughboost::core::demangle. It can also be piped through the command-line utilityc++filt-t.

      [edit]Example

      Run this code
      #include <boost/core/demangle.hpp>#include <cstdlib>#include <iostream>#include <string>#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'; std::string real_name= boost::core::demangle(typeid(pb).name());std::cout<<typeid(pb).name()<<" => "<< real_name<<'\n'; std::cout<<"c++filt => "<<std::flush;std::string s=typeid(pb).name();std::system(("c++filt -t "+ s).data());}

      Possible output:

      // GCC/Clang:4Base7DerivedPK4Base => Base const*c++filt => Base const* // MSVC:struct Basestruct Derivedstruct Base const * __ptr64 => struct Base const * __ptr64

      [edit]See also

      (C++11)
      returns a value which is identical for the same types
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/type_info/name&oldid=175774"

      [8]ページ先頭

      ©2009-2025 Movatter.jp