Type traits are a clever technique used in C++ template metaprogramming that gives you the ability to inspect and transform the properties of types.

For example, given a generic type T — it could be intboolstd::vector or whatever you want — with type traits you can ask the compiler some questions: is it an integer? Is it a function? Is it a pointer? Or maybe a class? Does it have a destructor? Can you copy it? Will it throw exceptions? … and so on. This is extremely useful in conditional compilation, where you instruct the compiler to pick the right path according to the type in input. We will see an example shortly.

Type traits can also apply some transformation to a type. For example, given T, you can add/remove the const specifier, the reference or the pointer, or yet turn it into a signed/unsigned type and many other crazy operations. Extremely handy when writing libraries that make use of templates.

The beauty of these techniques is that everything takes place at compile time with no runtime penalties: it’s template metaprogramming, after all. I assume you know a bit about C++ templates for the rest of this article. This guide is a great introduction if you don’t.x

adapted from asb

References

  1. A quick primer on type traits in modern C++
  2. C++ Type traits