`typeid` — C++ Keyword
`typeid` — C++ Keyword
The typeid keyword in C++: queries the runtime type of an expression.
`typeid` — C++ Keyword
The typeid keyword in C++: queries the runtime type of an expression.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
typeidA compile-time and runtime operator that returns a std::type_info reference describing the type or dynamic type of an expression. Defined in <typeinfo>.
typeid(expression)
typeid(type-name)
#include <print>
#include <typeinfo>
#include <memory>
struct Animal { virtual ~Animal() = default; };
struct Dog : Animal {};
struct Cat : Animal {};
int main() {
// Static type query
int n = 0;
std::println("{}", typeid(n).name()); // "i" (int) on GCC
// Dynamic type query through base pointer
std::unique_ptr<Animal> a = std::make_unique<Dog>();
if (typeid(*a) == typeid(Dog)) {
std::println("It's a Dog"); // executed
}
std::unique_ptr<Animal> b = std::make_unique<Cat>();
std::println("{}", typeid(*b).name()); // Cat's mangled name
}
virtual function in the base class.name() string is implementation-defined; use std::demangle (a GCC/Clang extension) or boost::core::demangle for readable names.dynamic_cast over typeid for conditional down-casting.typeidint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}