📜  C++中的std :: make_signed与示例

📅  最后修改于: 2021-05-30 04:10:36             🧑  作者: Mango

头文件中存在C++ STL的std :: make_signed模板。 C++ STL的std :: make_signed模板用于通过保留任何cv限定符来获取与T(Triat类)对应的带符号类型。可以使用std :: is_same :: value函数检查给定类型T是否为带符号类型。

头文件:

#include

模板类别:

template< class T >
struct make_signed;

template
using make_signed_t 
    = typename make_signed::type;

句法:

std::make_signed::type

参数:模板std :: make_signed接受单个参数T(Trait类) ,并将类型T设为有符号类型。

下面是演示C++中std :: make_signed的程序:

程序:

// C++ program to illustrate
// std::make_signed
// make_signed
#include 
#include 
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::type A;
    typedef make_signed::type B;
    typedef make_signed::type C;
    typedef make_signed::type D;
    typedef make_signed::type E;
  
    cout << boolalpha;
  
    // Check if the above declared variables
    // are signed type or not
    cout << "A is signed type? "
         << is_same::value
         << endl;
  
    cout << "B is signed type? "
         << is_same::value
         << endl;
  
    cout << "C is signed type? "
         << is_same::value
         << endl;
  
    cout << "D is signed type? "
         << is_same::value
         << endl;
  
    cout << "E is signed type? "
         << is_same::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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”