<type_traits>頭文件中提供了C++ STL的std::make_signed模板。 C++ STL的std::make_signed模板用於通過保留任何cv-qualifiers來獲取與T(Triat類)相對應的帶符號類型。可以使用std::is_same::value函數檢查給定類型T是否為帶符號類型。
頭文件:
#include<type_traits>
模板類別:
template< class T > struct make_signed; template<class T> using make_signed_t = typename make_signed<T>::type;
用法:
std::make_signed<T>::type
參數:模板std::make_signed接受單個參數T(Trait類),並將類型T設為有符號類型。
下麵是在C++中演示std::make_signed的程序:
程序:
// 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;
}
輸出:
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
參考: http://www.cplusplus.com/reference/type_traits/make_signed/
相關用法
- C語言 strtok()、strtok_r()用法及代碼示例
- C語言 memset()用法及代碼示例
- C++ std::mismatch()用法及代碼示例
- C++ wcscpy()用法及代碼示例
- C++ wcscmp()用法及代碼示例
- C++ set_symmetric_difference用法及代碼示例
- C++ ratio_equal()用法及代碼示例
- C++ std::equal_to用法及代碼示例
- C++ quick_exit()用法及代碼示例
- C++ multiset lower_bound()用法及代碼示例
- C++ multiset upper_bound()用法及代碼示例
- C++ multiset max_size()用法及代碼示例
- C++ forward_list max_size()用法及代碼示例
- C++ std::allocator()用法及代碼示例
- C++ array data()用法及代碼示例
- C++ multiset size()用法及代碼示例
- C++ ratio_not_equal()用法及代碼示例
注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 std::make_signed in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。