std::type_info::name(3)
β¨ NAME β¨
okay so bestie, welcome to the <hotpink>std::type_info::name(3)</hotpink> page from the C++ Standard Libary β yep, that's how the header spells it, and we keep the details π. The whole NAME section is literally std::type_info::name - std::type_info::name, and honestly? iconic minimalism π
. This is the function that gives you the name of a type, and the vibe check is: it's implementation defined, so don't expect universal main-character consistency across compilers π«Ά.
πΈ Synopsis πΈ
const char* name() const; (until C++11)
const char* name() const noexcept; (since C++11)
Translation: it 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 π. Like, it's giving chaos gremlin, not contract.
π Parameters π
(none) β literally no parameters, bestie, she takes nothing and returns vibes β¨.
π Return value π
Null-terminated character string containing the name of the type. That's the whole return value, no cap π§.
π₯ Notes π₯
Okay, important lore: 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). So like, usually fine, but don't be weird about it π.
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 the Itanium C++ ABI. The mangled name can be converted to human-readable form using implementation-specific API such as abi::__cxa_demangle directly or through boost::core::demangle. It can also be piped through the command-line utility c++filt -t.
π¦ Example π¦
Here's the code, copied exactly because we do not mess with code, periodt π :
// 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:
4Base
7Derived
PK4Base => Base const*
c++filt => Base const*
// MSVC:
struct Base
struct Derived
struct Base const * __ptr64 => struct Base const * __ptr64
π See also π
hash_codeβ returns a value which is identical for the same types (C++11) (public member function).
Sign-off time π«Ά: source/version/date realness β http://cppreference.com, 2024.06.10, manual page std::type_info::name(3) β all accounted for, mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms