std::make_signed in C++ with Examples

The std::make_signed template of C++ STL is present in the <type_traits> header file. The std::make_signed template of C++ STL is used to get the signed type corresponding to T(Triat class), by keeping any cv-qualifiers. It can be checked using std::is_same::value function whether the given type T is signed type or not.

Header File:

#include<type_traits>

Template Class:

template< class T >
struct make_signed;

template<class T>
using make_signed_t 
    = typename make_signed<T>::type;

Syntax:

std::make_signed<T>::type

Parameter: The template std::make_signed accepts a single parameter T(Trait class) and maked the type T as a signed type.



Below is the program to demonstrate std::make_signed in C++:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::make_signed
// make_signed
#include <iostream>
#include <type_traits>
using namespace std;
  
// Declare enum
enum ENUM1 { a,
             b,
             c };
  
enum class ENUM2 : unsigned char { x,
                                   y,
                                   z };
  
// Driver Code
int main()
{
  
    // Declare variable using make_signed
    // for int, unsigned, const unsigned,
    // enum1 and enum2
    typedef make_signed<int>::type A;
    typedef make_signed<unsigned>::type B;
    typedef make_signed<const unsigned>::type C;
    typedef make_signed<ENUM1>::type D;
    typedef make_signed<ENUM2>::type E;
  
    cout << boolalpha;
  
    // Check if the above declared variables
    // are signed type or not
    cout << "A is signed type? "
         << is_same<int, A>::value
         << endl;
  
    cout << "B is signed type? "
         << is_same<int, B>::value
         << endl;
  
    cout << "C is signed type? "
         << is_same<int, C>::value
         << endl;
  
    cout << "D is signed type? "
         << is_same<int, D>::value
         << endl;
  
    cout << "E is signed type? "
         << is_same<int, E>::value
         << endl;
  
    return 0;
}

chevron_right


Output:

A is signed type? true
B is signed type? true
C is signed type? false
D is signed type? true
E is signed type? false

Reference: http://www.cplusplus.com/reference/type_traits/make_signed/

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.