
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
is_arithmetic Template in C++
In this article we will be discussing the working, syntax and examples of std::is_arithmetic template in C++ STL.
is_arithmetic template helps to check whether the given class T is of arithmetic type or not.
What is an Arithmetic Type?
Arithmetic type consists of two types, that are
integral types − In this we define the whole numbers. The following are the type of integral types −
- char
- bool
- int
- long
- short
- long long
- wchar_t
- char16_t
- char32_t
floating point types − These can hold fractional parts. The following are the type of floating point.
- Float
- Double
- Long double
So, template is_arithmatic checks the defined type T is an arithmetic type or not and returns true or false accordingly.
Syntax
template <class T> is_arithmetic;
Parameters
A template can have only one parameter that is of type T, and checks the parameter is of arithmetic type or not.
Return value
This function returns a bool type value, that can be either true or false. It returns true if the given type is arithmetic and false if the type is not arithmetic.
Example
Input: is_arithmetic<bool>::value; Output: True Input: is_arithmetic<class_a>::value; Output: false
Example
#include <iostream> #include <type_traits> using namespace std; class TP { }; int main() { cout << boolalpha; cout << "checking for is_arithmetic template:"; cout << "\nTP class : "<< is_arithmetic<TP>::value; cout << "\n For Bool value: "<< is_arithmetic<bool>::value; cout << "\n For long value : "<< is_arithmetic<long>::value; cout << "\n For Short value : "<< is_arithmetic<short>::value; return 0; }
Output
If we run the above code it will generate the following output −
checking for is_arithmetic template: TP class : false For Bool value: true For long value : true For Short value : true
Example
#include <iostream> #include <type_traits> using namespace std; int main() { cout << boolalpha; cout << "checking for is_arithmetic template:"; cout << "\nInt : "<< is_arithmetic<int>::value; cout << "\nchar : "<< is_arithmetic<char>::value; cout << "\nFloat : "<< is_arithmetic<float>::value; cout << "\nDouble : "<< is_arithmetic<double>::value; cout << "\nInt *: "<< is_arithmetic<int*>::value; cout << "\nchar *: "<< is_arithmetic<char*>::value; cout << "\nFloat *: "<< is_arithmetic<float*>::value; cout << "\nDouble *: "<< is_arithmetic<double*>::value; return 0; }
Output
If we run the above code it will generate the following output −
checking for is_arithmetic template: Int : true Char : true Float : true Double : true Int * : float Char *: float Float *: float Double *: float
- Related Articles
- Template Metaprogramming in C++
- Template Specialization in C++
- is_final template in C++
- is_fundamental Template in C++
- is_pod template in C++
- is_pointer Template in C++
- is_signed template in C++
- is_unsigned template in C++
- is_void template in C++
- is_standard_layout template in C++
- is_class template in C++
- is_const Template in C++
- is_lvalue_reference Template in C++
- is_rvalue_reference Template in C++
- is_empty template in C++
