Tuesday, August 22, 2017

C++11: type traits

Several new type traits were added to the language. Type traits are defined in the header file. The purpose of type traits is to get or set the properties of types.” Here is a list of added Primary type categories to C++11:

is_void
is_integral
is_floating_point
is_array
is_enum
is_union
is_class
is_function
is_pointer
is_lvalue_reference
is_rvalue_reference
is_member_object_pointer
is_member_function_pointer
Here is an example:

#include <iostream>
#include <type_traits>

class C {};
enum E {};
union U {};
typedef int MyInt;
typedef double MyDouble;

int main()
{
  std::cout << std::is_class<C>                ::value << " ";
  std::cout << std::is_enum<E>                 ::value << " ";
  std::cout << std::is_union<U>                ::value << " ";
  std::cout << std::is_integral<MyInt>         ::value << " ";
  std::cout << std::is_floating_point<MyDouble>::value << " ";
  std::cout << std::is_class<E>                ::value << " ";
  std::cout << std::is_enum<C>                 ::value << std::endl;

  return 0;
}
// Output: 1 1 1 1 1 0 0
Reference: http://en.cppreference.com/w/cpp/types

No comments:

Post a Comment