ZeePedia

Appendix. Type Traits:Helper Class, Primary Type Categories, Binary Type Traits

<< Standard Template Library C++:adjacent_find, copy_backward, lexicographical_compare
Notices:References, Bug Reports >>
img
Appendix. Type Traits
A library of type traits has been approved for inclusion in the Technical Report on
C++ Library Extensions (TR1). This library may be proposed for inclusion in a
revision to the ISO C++ Standard in the future. XL C++ V8 supports the use of the
TR1 Type Traits library.
The TR1 Type Traits Library is declared in the nested namespace std::tr1. This
library must be explicitly enabled by defining the macro __IBMCPP_TR1__. If you
include the header file <type_traits> without defining the macro __IBMCPP_TR1__
to a non-zero integer value, the preprocessor will issue an error message.
For more information about the Technical Report 1 see, Proposed Draft Technical
Report on C++ Library Extensions. (ISO/IEC Doc Number N1745).
Note: While it is likely that the Type Traits library will be approved for inclusion
to a future revision to the ISO C++ Standard, it is impossible to predict, at
this time, the precise form that the library will take when it is adopted for
standardization. User programs that make use of the TR1 Type Traits
Library today may not be completely source-code compatible with a
Standard Type Traits Library, should one be approved by ISO.
Implementation Notes
1. The circumstances under which these type traits yield a result of trueis not
specified in TR1:
v is_empty
v is_pod
v has_trivial_constructor
v has_trivial_copy
v has_trivial_assign
v has_trivial_destructor
v has_nothrow_destructor
v has_nothrow_copy
v has_nothrow_assign
With XL C++, these traits behave as specified in the following section.
2. TR1 grants to implementors of the type traits library the latitude to implement
certain type traits as class templates with no static or non-static data or
function members, no base classes, and no nested types. For example, the
following implementations of the type traits is_class and is_union are
permissible for implementations that cannot distinguish between class and
union types:
template <typename T> struct is_class{};
template <typename T> struct is_union{};
The type traits for which this latitude is granted are:
v is_class
v is_union
v is_polymorphic
is_abstract
v
415
img
XL C++ does not take advantage of this latitude. Full implementations of these
type traits are provided
3. TR1 grants to implementors of the type traits library the latitude to implement
the type trait has_virtual_destructor in such a way that its static data member
always has a value of true, regardless of the type argument to which it is
applied. XL C++ does not take advantage of this latitude. The expression
has_virtual_destructor<T>::value will have a value of true if and only if the
type argument T is a class type with a virtual destructor.
Header file <type_traits>
namespace std {
namespace tr1 {
template <class _T, _T _V>
struct integral_constant;
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
// Unary
Type Traits
template
<class _T> struct
is_void;
template
<class _T> struct
is_integral;
template
<class _T> struct
is_floating_point;
template
<class _T> struct
is_array;
template
<class _T> struct
is_pointer;
template
<class _T> struct
is_reference;
template
<class _T> struct
is_member_object_pointer;
template
<class _T> struct
is_member_function_pointer;
template
<class _T> struct
is_enum;
template
<class _T> struct
is_union;
template
<class _T> struct
is_class;
template
<class _T> struct
is_function;
template
<class
_T>
struct
is_arithmetic;
template
<class
_T>
struct
is_fundamental;
template
<class
_T>
struct
is_object;
template
<class
_T>
struct
is_scalar;
template
<class
_T>
struct
is_compound;
template
<class
_T>
struct
is_member_pointer;
template
<class
_T>
struct
is_const;
template
<class
_T>
struct
is_volatile;
template
<class
_T>
struct
is_pod;
template
<class
_T>
struct
is_empty;
template
<class
_T>
struct
is_polymorphic;
template
<class
_T>
struct
is_abstract;
template
<class
_T>
struct
has_trivial_constructor;
template
<class
_T>
struct
has_trivial_copy;
template
<class
_T>
struct
has_trivial_assign;
template
<class
_T>
struct
has_trivial_destructor;
template
<class
_T>
struct
has_nothrow_constructor;
template
<class
_T>
struct
has_nothrow_copy;
template
<class
_T>
struct
has_nothrow_assign;
template
<class
_T>
struct
has_virtual_destructor;
template
<class
_T>
struct
is_signed;
template
<class
_T>
struct
is_unsigned;
template <class _T> struct alignment_of;
template <class _T> struct rank;
template <class _T, unsigned _I = 0> struct extent;
// Binary Type Traits
template <class _T, class _U> struct is_same;
template <class _From, class _To> struct is_convertible;
416
Standard C++ Library
img
template <class _From, class _To> struct is_base_of;
// Transformation Type Traits
template <class _T> struct remove_const;
template <class _T> struct remove_volatile;
template <class _T> struct remove_cv;
template <class _T> struct add_const;
template <class _T> struct add_volatile;
template <class _T> struct add_cv;
template <class _T> struct remove_reference;
template <class _T> struct add_reference;
template <class _T> struct remove_pointer;
template <class _T> struct add_pointer;
template <class _T> struct remove_extent;
template <class _T> struct remove_all_extents;
template <std::size_t _Len, std::size_t _Align> struct aligned_storage;
Helper Class
template <class _T, _T _V>
struct integral_constant
{
static  const _T
value = _V;
typedef _T
value_type;
typedef integral_constant<_T,_V> type;
};
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
The class template integral_constant and its associated typedefs
integral_constant and integral_constant are used as base classes to define the
interface for various type traits
The Traits
Each of the various type traits in the TR1 Type Traits Library falls into exactly one
of three categories:
v UnaryTypeTraits that describes a property of a single type.
v BinaryTypeTraits that describes a relationship between two types.
v TransformationTypeTraits that modify a property of a type.
Unary Type Traits
Every Unary Type Trait possesses a static data member named value. For most
traits, this member has type bool, and indicates the presence or absence of a
specific property or trait of the argument type. For example, the value of the
following expression will be true if the type argument T is a union type, and false
otherwise:
std::tr1::is_union<T>::value
A few of the Unary Type Traits possess a static data member named value whose
type is not bool. An example is the type trait extent, which gives the number of
elements in an array type:
typedef char arr[42];
size_t sz = std::tr1::extent<arr>::value; //sz == 42;
Every instance of a Unary Type Trait is derived from an instance of
integral_constant. All Unary Type Traits are default-constructible.
417
Appendix. Type Traits
Primary Type Categories
A given type T will satisfy one of the following categories.
is_void
template <class _T> struct is_void;
std::tr1::is_void<T>::value == true if and only if T is one of the following
types:
v [const][volatile] void
is_integral
template <class _T> struct is_integral;
std::tr1::is_integral<T>::value == true if and only if T is one of the following
types:
[const]
[volatile]
bool
v
[const]
[volatile]
char
v
[const]
[volatile]
signed char
v
[const]
[volatile]
unsigned char
v
[const]
[volatile]
wchar_t
v
[const]
[volatile]
short
v
[const]
[volatile]
int
v
[const]
[volatile]
long
v
[const]
[volatile]
long long
v
[const]
[volatile]
unsigned short
v
[const]
[volatile]
unsigned int
v
[const]
[volatile]
unsigned long
v
[const]
[volatile]
unsigned long long
v
is_floating_point
template <class _T> struct is_floating_point;
std::tr1::is_floating_point<T>::value == true if and only if T is one of the
following types:
v [const] [volatile] float
v [const] [volatile] double
v [const] [volatile] long double
is_array
template <class _T> struct is_array;
std::tr1::is_array<T>::value == true if and only if T is an array type.
is_pointer
template <class _T> struct is_pointer;
std::tr1::is_point<T>::value == true if and only if T is a pointer type. This
category includes function pointer types, but not pointer to member types.
is_reference
template <class _T> struct is_reference;
418
Standard C++ Library
std::tr1::is_reference<T>::value == true if and only if T is a reference type.
This category includes reference to function types.
is_member_object_pointer
template <class _T> struct is_member_object_pointer;
std::tr1::is_member_object_pointer<T>::value == true if and only if T is a
pointer to data member type.
is_member_function_pointer
template <class _T> struct is_member_function_pointer;
std::tr1::is_member_function_pointer<T>::value == true if and only if T is a
pointer to member function type.
is_enum
template <class _T> struct is_enum;
std::tr1::is_enum<T>::value == true if and only if T is an enumeration type.
is_union
template <class _T> struct is_union;
std::tr1::is_union<T>::value == true if and only if T is a union type.
is_class
template <class _T> struct is_class;
std::tr1::is_class<T>::value == true if and only if T is a class or struct type
(and not a union type).
is_function
template <class _T> struct is_function;
std::tr1::is_function<T>::value == true if and only if T is a function type.
Composite Type Traits
is_arithmetic
template <class _T> struct is_arithmetic;
For a given type T, std::tr1::is_arithmetic<T>::value == true if and only if:
v std::tr1::is_floating_point<T>::value == true, or
v std::tr1::is_integral<T>::value == true
is_fundamental
template <class T> struct is_fundamental;
For a given type T, std::tr1::is_fundamental<T>::value == true if and only if:
v std::tr1::is_arithmetic<T>::value == true, or
v std::tr1::is_void<T>::value == true
419
Appendix. Type Traits
is_object
template <class T> struct is_object;
For a given type T, std::tr1::is_object<T>::value == true if and only if:
v std::tr1::is_reference<T>::value == false, and
v std::tr1::is_function<T>::value == false, and
v std::tr1::is_void<T>::value == false
is_scalar
template <class T> struct is_scalar;
For a given type T, std::tr1::is_scalar<T>::value == true if and only if:
v std::tr1::is_arithmetic<T>::value == true, or
v std::tr1::is_enum<T>::value == true, or
v std::tr1::is_pointer<T>::value == true, or
v std::tr1::is_member_pointer<T>::value
is_compound
template <class _T> struct is_compound;
For any type T, the following expression is true:
std::tr1::is_compound<T>::value != std::tr1::is_fundamental<T>::value
is_member_pointer
template <class _T> struct is_member_pointer;
For a given type T, std::tr1::is_member_pointer<T>::value == true if and only
if:
v std::tr1::is_member_object_pointer<T>::value == true, or
v std::tr1::is_member_function_pointer<T>::value == true
Type Properties
is_const
template <class _T> struct is_const;
std::tr1::is_const<T>::value == true if and only if T has const-qualification.
is_volatile
template <class _T> struct is_volatile;
std::tr1::is_volatile<T>::value == true if and only if T has
volatile-qualification.
is_pod
template <class _T> struct is_pod;
std::tr1::is_volatile<T>::value == true if and only if, for a given type T:
v std::tr1::is_scalar<T>::value == true, or
v T is a class or struct that has no user-defined copy assignment operator or
destructor, and T has no non-static data members M for which is_pod<M>::value
== false, and no members of reference type, or
420
Standard C++ Library
v T is a class or struct that has no user-defined copy assignment operator or
destructor, and T has no non-static data members M for which is_pod<M>::value
== false, and no members of reference type, or
v T is the type of an array of objects E for which is_pod<E>::value == true
is_pod may only be applied to complete types.
is_empty
template <class _T> struct is_empty;
std::tr1::is_empty<T>::value == true if and only if T is an empty class or struct.
is_empty may only be applied to complete types.
is_polymorphic
template <class _T> struct is_polymorphic;
std::tr1::is_polymorphic<T>::value == true if and only if T is a class or struct
that declares or inherits a virtual function. is_polymorphic may only be applied to
complete types.
is_abstract
template <class _T> struct is_abstract;
std::tr1::is_abstract<T>::value == true if and only if T is a class or struct that
has at least one pure virtual function. is_abstract may only be applied to
complete types.
has_trivial_constructor
template <class _T> struct has_trivial_constructor;
std::tr1::has_trivial_constructor<T>::value == true if and only if T is a class
or struct that has a trivial constructor. A constructor is trivial if
v it is implicitly defined by the compiler, and
v is_polymorphic<T>::value == false, and
v T has no virtual base classes, and
v for every direct base class of T, has_trivial_constructor<B>::value == true,
where B is the type of the base class, and
v for every nonstatic data member of T that has class type or array of class type,
has_trivial_constructor<M>::value == true, where M is the type of the data
member
has_trivial_constructor may only be applied to complete types.
has_trivial_copy
template <class _T> struct has_trivial_copy;
std::tr1::has_trivial_copy<T>::value == true if and only if T is a class or struct
that has a trivial copy constructor. A copy constructor is trivial if
v it is implicitly defined by the compiler, and
v is_polymorphic<T>::value == false, and
v T has no virtual base classes, and
v for every direct base class of T, has_trivial_copy<B>::value == true, where B is
the type of the base class, and
v for every nonstatic data member of T that has class type or array of class type,
has_trivial_copy<M>::value == true, where M is the type of the data member
421
Appendix. Type Traits
has_trivial_copy may only be applied to complete types.
has_trivial_assign
template <class _T> struct has_trivial_assign;
std::tr1::has_trivial_assign<T>::value == true if and only if T is a class or
struct that has a trivial copy assignment operator. A copy assignment operator is
trivial if:
v it is implicitly defined by the compiler, and
v is_polymorphic<T>::value == false, and
v T has no virtual base classes, and
v for every direct base class of T, has_trivial_assign<B>::value == true, where B
is the type of the base class, and
v for every nonstatic data member of T that has class type or array of class type,
has_trivial_assign<M>::value == true, where M is the type of the data member
has_trivial_assign may only be applied to complete types.
has_trivial_destructor
template <class _T> struct has_trivial_destructor;
std::tr1::has_trivial_destructor<T>::value == true if and only if T is a class or
struct that has a trivial destructor. A destructor is trivial if
v it is implicitly defined by the compiler, and
v for every direct base class of T, has_trivial_destructor<B>::value == true,
where B is the type of the base class, and
v for every nonstatic data member of T that has class type or array of class type,
has_trivial_destructor<M>::value == true, where M is the type of the data
member
has_trivial_destructor may only be applied to complete types.
has_nothrow_constructor
template <class _T> struct has_nothrow_constructor;
std::tr1::has_nothrow_constructor<T>::value == true if and only if T is a class
or struct whose default constructor has an empty throw specification.
has_nothrow_constructor may only be applied to complete types.
has_nothrow_copy
template <class _T> struct has_nothrow_copy;
std::tr1::has_nothrow_copy<T>::value == true if and only if T is a class or struct
whose copy constructor has an empty throw specification.
has_nothrow_copy may only be applied to complete types.
has_nothrow_assign
template <class _T> struct has_nothrow_assign;
std::tr1::has_nothrow_assign<T>::value == true if and only if T is a class or
struct whose copy assignment operator has an empty throw specification.
has_nothrow_assign may only be applied to complete types.
422
Standard C++ Library
has_virtual_destructor
template <class _T> struct has_virtual_destructor;
std::tr1::has_virtual_destructor<T>::value == true if and only if T is a class or
struct with a virtual destructor.
has_virtual_destructor may only be applied to complete types.
is_signed
template <class _T> struct is_signed;
std::tr1::is_signed<T>::value == true if and only if T is one of the following
types:
v [const] [volatile] signed char
v [const] [volatile] short
v [const] [volatile] int
v [const] [volatile] long
v [const] [volatile] long long
is_unsigned
template <class _T> struct is_unsigned;
std::tr1::is_unsigned<T>::value == true if and only if T is one of the following
types:
v [const] [volatile] unsigned char
v [const] [volatile] unsigned short
v [const] [volatile] unsigned int
v [const] [volatile] unsigned long
v [const] [volatile] unsigned long long
alignment_of
template <class _T> struct alignment_of;
std::tr1::alignment_of<T>::value is an integral value representing, in bytes, the
memory alignment of objects of type T.
alignment_of may only be applied to complete types.
rank
template <class _T> struct rank;
std::tr1::rank<T>::value is an integral value representing the number of
dimensions possessed by an array type. For example, given a multi-dimensional
array type T[M][N], std::tr1::rank<T[M][N]>::value == 2. For a given non-array
type T, std::tr1::rank<T>::value == 0.
extent
template <class _T, unsigned _I = 0> struct extent;
std::tr1::extent<T, I>::value is an integral type representing the number of
elements in the Ith dimension of array type T.
For a given array type T[N], std::tr1::extent<T[N]>::value == N.
423
Appendix. Type Traits
img
For a given multi-dimensional array type T[M][N], std::tr1::extent<T[M][N],
0>::value == N.
For a given multi-dimensional array type T[M][N], std::tr1::extent<T[M][N],
1>::value == M.
For a given array type T and a given dimension I where I >= rank<T>::value,
std::tr1::extent<T, I>::value == 0.
For a given array type of unknown extent T[], std::tr1::extent<T[], 0>::value
== 0.
For a given non-array type T and an arbitrary dimension I, std::tr1::extent<T,
I>::value == 0.
Binary Type Traits
Binary Type Traits provide information about a relationship between two types.
Every Binary Type Trait possesses a static data member of type bool named value.
This member indicates the presence or absence of a specific relationship between
the two argument types. For example, the value of the following expression will be
true if the type arguments T and S are the same type, and false otherwise:
std::tr1::is_same<T, S>::value
is_same
template <class _T, class _U> struct is_same;
Given two (possibly identical) types T and S, std::tr1::is_same<T, S>::value ==
true if and only if T and S are the same type.
is_convertible
template <class _From, class _To> struct is_convertible;
Given two (possible identical) types From and To, std::tr1::is_convertible<From,
To>::value == true if and only if an lvalue of type From can be implicitly
converted to type To, or is_void<To>::value == true
is_convertible may only be applied to complete types. Type To may not be an
abstract type. If the conversion is ambiguous, the program is ill-formed. If either or
both of From and To are class types, and the conversion would invoke non-public
member functions of either From or To (such as a private constructor of To, or a
private conversion operator of From), the program is ill-formed.
is_base_of
template <class _Base, class _Derived> struct is_base_of;
Given two (possibly identical) types Base and Derived,
std::tr1::is_base_of<Base, Derived>::value == true if and only if Base is a
direct or indirect base class of Derived, or Base and Derived are the same type.
is_base_of may only be applied to complete types.
424
Standard C++ Library
img
Relationships Between Types
Transformation Type Traits modify a type. Every Transformation Type Trait
possesses a nested typedef named type that represents the result of the
modification. For example, for a given type T, the type
std::tr1::add_reference<T>::type is equivalent to the type T &.
remove_const
template <class _T> struct remove_const;
The remove_const transformation trait removes top-level const qualification (if any)
from the type to which it is applied. For a given type T, std::tr1::remove_const<T
const>::type is equivalent to the type T. For example,
std::tr1::remove_const<char>::type is equivalent to char * while
std::tr1::remove_const<const char>::type is equivalent to const char *. In the
latter case, the const qualifier modifies char, not *, and is therefore not at the top
level.
remove_volatile
template <class _T> struct remove_volatile;
The remove_volatile transformation trait removes top-level volatile qualification
(if any) from the type to which it is applied. For a given type T, the type
std::tr1::remove_volatile <T volatile>::T is equivalent to the type T. For
example, std::tr1::remove_volatile <char * volatile>::type is equivalent to
char * while std::tr1::remove_volatile <volatile char>::type is equivalent to
volatile char *. In the latter case, the volatile qualifier modifies char, not *, and
is therefore not at the top level.
remove_cv
template <class _T> struct remove_cv;
The remove_cv transformation trait removes top-level const and/or volatile
qualification (if any) from the type to which it is applied. For a given type T,
std::tr1::remove_cv<T const volatile>::type is equivalent to T. For example,
std::tr1::remove_cv<char * volatile>::type is equivalent to char *, while
std::tr1::remove_cv*lt;const char *>::type is equivalent to const char *. In
the latter case, the const qualifier modifies char, not *, and is therefore not at the
top level.
add_const
template <class _T> struct add_const;
The add_const transformation trait adds const qualification to the type to which it
is applied.
For a given type T, std::tr1::add_const<T>::type is equivalent to T const if
is_const<T>::value == false, and
v is_void<T>::value == true, or
v is_object<T>::value == true.
Otherwise, std::tr1::add_const<T>::type is equivalent to T.
425
Appendix. Type Traits
add_volatile
template <class _T> struct add_volatile;
The add_volatile transformation trait adds volatile qualification to the type to
which it is applied.
For a given type T, std::tr1::add_volatile<T>::type is equivalent to T volatile
if is_volatile<T>::value == false, and
v is_void<T>::value == true, or
v is_object<T>::value == true.
Otherwise, std::tr1::add_volatile<T>::type is equivalent to T.
add_cv
template <class _T> struct add_cv;
The add_cv transformation trait adds const and volatile qualification to the type
to which it is applied. For a given type T, std::tr1::add_volatile<T>::type is
equivalent to std::tr1::add_const<std::tr1::add_volatile<T>::type>::type.
remove_reference
template <class _T> struct remove_reference;
The remove_reference transformation trait removes top-level of indirection by
reference (if any) from the type to which it is applied. For a given type T,
std::tr1::remove_reference<T &>::type is equivalent to T.
add_reference
template <class _T> struct add_reference;
The add_reference transformation trait adds a level of indirection by reference to
the type to which it is applied. For a given type T,
std::tr1::add_reference<T>::type is equivalent to T & if is_reference<T>::value
== false, and T otherwise.
remove_pointer
template <class _T> struct remove_pointer;
The remove_pointer transformation trait removes top-level indirection by pointer
(if any) from the type to which it is applied. Pointers to members are not affected.
For a given type T, std::tr1::remove_pointer<T *>::type is equivalent to T.
add_pointer
template <class _T> struct add_pointer;
The add_pointer transformation trait adds a level of indirection by pointer to the
type to which it is applied.
For a given type T, std::tr1::add_pointer<T>::type is equivalent to T * if
is_reference<T>::value == false, and std::tr1::remove_reference<T>::type *,
otherwise.
426
Standard C++ Library
remove_extent
template <class _T> struct remove_extent;
The remove_extent transformation trait removes a dimension from an array.
For a given non-array type T, std::tr1::remove_extent<T>::type is equivalent to
T.
For a given array type T[N], std::tr1::remove_extent<T[N]>::type is equivalent to
T.
For a given array type const T[N], std::tr1::remove_extent<const T[N]>::type is
equivalent to const T.
For example, given a multi-dimensional array type T[M][N],
std::tr1::remove_extent<T[M][N]>::type is equivalent to T[N].
remove_all_extents
template <class _T> struct remove_all_extents;
The remove_all_extents transformation trait removes all dimensions from an array.
For a given non-array type T, std::tr1::remove_all_extents<T>::type is
equivalent to T.
For a given array type T[N], std::tr1::remove_all_extents<T[N]>::type is
equivalent to T.
For a given array type const T[N], std::tr1::remove_all_extents<const
T[N]>::type is equivalent to const T.
For example, given a multi-dimensional array type T[M][N],
std::tr1::remove_all_extents<T[M][N]>::type is equivalent to T.
aligned_storage
template <std::size_t _Len, std::size_t _Align> struct aligned_storage;
The aligned_storage transformation trait provides a type that is suitably aligned
to store an object whose size is does not exceed _Len and whose alignment is a
divisor of _Align. When using aligned_storage, _Len must be non-zero, and
_Align must be equal to alignment_of<T>::value for some type T.
427
Appendix. Type Traits
428
Standard C++ Library