Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ standard library

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:

Syntax

The templates in it usually use the std:: prefix; for example, std::is_integral::value 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:

Type modifiers:

Type Conversion:

Type trait detection:

Conditional type:

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.