The C++ standard library
is a very useful header file in the C++ standard library; it contains a set of tools for checking type traits at compile time. These tools help developers determine type traits at compile time, enabling safer and more flexible code.
The header defines a set of templates that can be used to query and manipulate type attributes. These attributes include, but are not limited to:
- Whether it is an integer type
- Whether it is a floating-point type
- Whether it is a pointer type
- Whether it is a reference type
- Whether it is callable (function or function pointer)
Syntax
The templates in it usually use the std:: prefix; for example, std::is_integral is used to check whether type T is an integer type. Here, value is a static constant whose value is true or false.
Here are some commonly used type_traits Function:
Basic type judgment:
std::is_void: Determine whether typeTisvoid.std::is_integral: Determine whether typeTan integer type.std::is_floating_point: Determine whether typeTa floating-point type.std::is_array: Determine whether typeTan array type.std::is_pointer: Determine whether typeTa pointer type.std::is_reference: Determine whether typeTa reference type.std::is_const: Determine whether typeTisconstmodifier.
Type modifiers:
std::remove_const: Remove typeT'sconstmodifier.std::remove_volatile: Remove typeT'svolatilemodifier.std::remove_cv: Also remove typeT'sconstandvolatilemodifier.std::remove_reference: Remove typeT's reference modifier.std::remove_pointer: Remove typeT's pointer modifier.
Type Conversion:
std::add_const: Add for typeTaddconstmodifier.std::add_volatile: Add for typeTaddvolatilemodifier.std::add_cv: Also add for typeTaddconstandvolatilemodifier.std::add_pointer: Add for typeTa pointer modifier.std::add_lvalue_reference: Add for typeTAdd an lvalue reference modifier.std::add_rvalue_reference: Add for typeTAdd an rvalue reference modifier.
Type trait detection:
std::is_same: Determine whether typeTandUWhether they are the same.std::is_base_of: Determine whether typeBaseWhether it is typeDerived's base class.std::is_convertible: Determine whether typeFromWhether it can be converted to typeTo.
Conditional type:
std::conditional: IfConditionistrue, then the type isT; otherwise, it isF.std::enable_if: IfConditionistrue, then the type isT; otherwise this template does not participate in overload resolution.
Example
The following are some uses of examples, along with their output results.
Check whether it is an integer type
Example
#include
#include
int
main
(
)
{
std
::
cout
<<
"int is integral: "
<<
std
::
is_integral
<
int
::
value
<<
std
::
endl
;
std
::
cout
<<
"float is integral: "
<<
std
::
is_integral
<
float
::
value
<<
std
::
endl
;
std
::
cout
<<
"char is integral: "
<<
std
::
is_integral
<
char
::
value
<<
std
::
endl
;
return
0
;
}
Output result:
int is integral: 1
float is integral: 0
char is integral: 1
Check whether it is a floating-point type
Example
#include
#include
int
main
(
)
{
std
::
cout
<<
"int is floating_point: "
<<
std
::
is_floating_point
<
int
::
value
<<
std
::
endl
;
std
::
cout
<<
"float is floating_point: "
<<
std
::
is_floating_point
<
float
::
value
<<
std
::
endl
;
std
::
cout
<<
"double is floating_point: "
<<
std
::
is_floating_point
<
double
::
value
<<
std
::
endl
;
return
0
;
}
Output result:
int is floating_point: 0
float is floating_point: 1
double is floating_point: 1
Check whether it is a pointer type
Example
#include
#include
int
main
(
)
{
int
a
=
10
;
int
p
=
&
a
;
std
::
cout
<<
"int* is a pointer: "
<<
std
::
is_pointer
<
int
::
value
<<
std
::
endl
;
std
::
cout
<<
"int is a pointer: "
<<
std
::
is_pointer
<
int
::
value
<<
std
::
endl
;
return
0
;
}
Output result:
int* is a pointer: 1
int is a pointer: 0
Check whether it is a reference type
Example
#include
#include
int
main
(
)
{
int
a
=
10
;
int
&
ref
=
a
;
std
::
cout
<<
"int& is a reference: "
<<
std
::
is_reference
<
int
&::
value
<<
std
::
endl
;
std
::
cout
<<
"int is a reference: "
<<
std
::
is_reference
<
int
::
value
<<
std
::
endl
;
return
0
;
}
Output result:
int& is a reference: 1
int is a reference: 0
Check whether it is callable
Example
#include
#include
#include
void
func
(
)
{
}
int
main
(
)
{
std
::
cout
<<
"int is callable: "
<<
std
::
is_callable
<
int
::
value
<<
std
::
endl
;
std
::
cout
<<
"void() is callable: "
<<
std
::
is_callable
<
void
(
)::
value
<<
std
::
endl
;
std
::
cout
<<
"func is callable: "
<<
std
::
is_callable
<
decltype
(
func
)::
value
<<
std
::
endl
;
return
0
;
}
Output result:
int is callable: 0
void() is callable: 1
func is callable: 1
is a very useful tool in C++; it allows developers to check and manipulate type attributes at compile time. This not only improves code safety but also makes the code more flexible and reusable.