
- 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_signed template in C++
In this article we will be discussing the working, syntax and examples of std::is_signed template in C++ STL.
is_ signed is a template which comes under <type_traits> header file. This template is used to check whether the given type T is a signed type or not.
What is a signed type?
These are the basic arithmetic types, which contains sign value with them. All arithmetic data types are either signed and unsigned.
Like we want to show the values in negative we use signed type.
Like: -1 is signed int and -1.009 is signed float.
By default all the types are signed to make them unsigned we have to prefix the data type by unsigned.
Syntax
template <class T> is_signed;
Parameters
The template can have only parameter of type T, and check whether T is a signed type or not.
Return value
It returns a Boolean value, true if the given type is a Signed type, and false if the given type is not a signed type.
Example
Input: is_signed<int>::value; Output: True Input: is_signed<unsigned int>::value; Output: False
Example
#include <iostream> #include <type_traits> using namespace std; class TP { }; enum TP_1 : int {}; enum class TP_2 : int {}; int main() { cout << boolalpha; cout << "checking for is_signed:"; cout << "\nint:" << is_signed<int>::value; cout << "\nTP:" << is_signed<TP>::value; cout << "\nTP_1:" << is_signed<TP_1>::value; cout << "\nTP_2:" << is_signed<TP_2>::value; return 0; }
Output
If we run the above code it will generate the following output −
checking for is_signed: Int: true TP: false TP_1: false TP_2: false
Example
#include <iostream> #include <type_traits> using namespace std; int main() { cout << boolalpha; cout << "checking for is_signed:"; cout << "\nfloat:" << is_signed<float>::value; cout << "\nSigned int:" << is_signed<signed int>::value; cout << "\nUnsigned int:" << is_signed<unsigned int>::value; cout << "\ndouble:" << is_signed<double>::value; return 0; }
Output
If we run the above code it will generate the following output −
checking for is_signed: Float: true Signed int: true Unsigned int: false Double: true
- Related Articles
- Signed binary integers
- Signed floating point numbers
- Template Metaprogramming in C++
- is_final template in C++
- is_fundamental Template in C++
- is_pod template in C++
- is_pointer 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++
