9

Hello I've seen many examples like this in Cppreference.com:

std::is_class<T>
std::is_integral

And so on. I know if I run the code for example I get true or false. But what is the point in that? e.g knowing the object is of class type or not?

#include <iostream>
#include <type_traits>

struct A {};
class B {};
enum class C {};

int main()
{
    std::cout << std::boolalpha;
    std::cout << std::is_class<A>::value << '\n';
    std::cout << std::is_class<B>::value << '\n';
    std::cout << std::is_class<C>::value << '\n';
    std::cout << std::is_class<int>::value << '\n';
}

The output:

true
true
false
false
  • I've searched all over for a real example using this (is_class, is_integral, is_arithmetic, ...) But all the tutorials show only the hopeless example: only true or false.

  • Could anyone help me with a small useful example using this templates?

Gyapti Jain
  • 4,056
  • 20
  • 40
Rami Yen
  • 165
  • 1
  • 6
  • 1
    A simple example -- `std::copy`. What if the types to `std::copy` are "simple", like an array of `int` or `char`? You would use `memcpy`, right? So how do you say "If the type is simple, use memcpy, else use a "slow" loop"? – PaulMcKenzie Nov 06 '19 at 18:15

2 Answers2

17

It's not for writing to the console, that's for sure.

More broadly you're asking: what is the point of type traits?

The answer is template metaprogramming. For example, I can create a template specialisation that does one thing for integral types, and another for non-integral types.

Aaron Bullman has a simple introduction to type traits, as does Jacek here.

In my opinion, most use of these things will be found buried within implementations of cool features and classes and utilities (i.e. in libraries) as part of the background machinery that makes it all work.

Further reading:

rightfold's answer on that first one gives a superb example of when traits are useful:

For example, an implementation of std::copy may use std::memcpy internally instead of an explicit loop when the iterators are pointers to PODs. This can be achieved with SFINAE.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • So you mean for example I can use them to disable some instances of templates for specific type for example it is logical to disable a templated-function `Power` or class when the argument type is not integral let's say std::string? – Rami Yen Nov 06 '19 at 18:11
  • 1
    @RamiYen, Yes, that's referred to as [SFINAE](https://en.cppreference.com/w/cpp/language/sfinae). – chris Nov 06 '19 at 18:12
7

It's for template meta programming. When you have no idea what type(s) the end-user will pass into the template. Sometimes it's to report errors, sometimes it's to specialise on the types passed. Sometimes it's a combination.

The examples seen on cppreference.com (eg https://en.cppreference.com/w/cpp/types/is_enum ) are very much over simplified and just show how to use the trait in a non-typical manner. You would almost never use these traits directly in a simple (non-template function or class).

Richard Critten
  • 2,138
  • 3
  • 13
  • 16