wiki:hpp-type-traits
Table of Contents
<type_traits>
<type_traits> provides type introspection and transformation templates: std::is_integral<T>, std::is_pointer<T>, std::remove_reference<T>, std::enable_if<Cond, T>, and many others. Use them to write generic code that adapts to type properties at compile time.
Concepts (C++20) often replace manual type trait checks, but traits remain useful for SFINAE and metaprogramming.
Example
This example queries type traits to detect whether types are integral or pointer types, enabling conditional behavior.
// compile: g++ -std=c++11 -o typetraitsexample typetraitsexample.cpp // run: ./typetraitsexample // description: type trait checks #include <type_traits> #include <iostream> template <typename T> void describe() { if (std::is_integral_v<T>) { std::cout << "integral type\n"; } if (std::is_pointer_v<T>) { std::cout << "pointer type\n"; } } int main() { describe<int>(); describe<int*>(); describe<double>(); return 0; }
wiki/hpp-type-traits.md · Last modified: by 127.0.0.1
