
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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