C++中typeid(var).name()输出的PKc具体含义是什么?
PKc Output from typeid(typeid(check).name()).name() Great question! Let’s break down exactly what’s going on here and decode that mysterious PKc output you’re seeing.
First, let’s clarify the chain of calls
- When you run
typeid(check).name(), you’re getting GCC’s mangled shorthand for the type ofcheck:'c'forchar,'d'fordouble, etc. - The critical detail you might have missed:
std::type_info::name()always returns aconst char*, no matter what typecheckis. This is a fixed part of the C++ standard.
Decoding PKc
GCC uses a name mangling system to pack complex type names into short strings. Let’s unpack PKc piece by piece:
P: Abbreviation for "pointer"K: Abbreviation for "const" (it comes from the German word Konstant, which is why it’s notC)c: Abbreviation forchar
Put it all together, PKc is GCC’s mangled way of writing const char* — which is exactly the type of the value returned by typeid(check).name(). That’s why changing check’s type doesn’t change this output: you’re always asking for the type of the same const char* pointer.
Verify with demangled output
If you want to see a human-readable version instead of the mangled string, you can use GCC’s built-in demangling function. Here’s a quick example:
#include <bits/stdc++.h> #include <cxxabi.h> int main() { double check = 69.666; const char* mangled_type = typeid(typeid(check).name()).name(); int demangle_status; char* demangled = abi::__cxa_demangle(mangled_type, nullptr, nullptr, &demangle_status); if (demangle_status == 0) { std::cout << "Demangled type: " << demangled << "\n"; free(demangled); // Clean up the allocated memory } else { std::cout << "Couldn't demangle the type name\n"; } return 0; }
Running this will output:
Demangled type: char const*
Which confirms our explanation perfectly.
内容的提问来源于stack exchange,提问作者pkdyn

