📜  C++中的is_const模板

📅  最后修改于: 2021-05-30 02:52:45             🧑  作者: Mango

C++ STL的std :: is_const模板用于检查类型是否为const限定。它返回一个显示相同值的布尔值。

语法

template  < class T >struct is_const;

模板参数:该模板包含单个参数T(Trait类),以检查T是否为const限定类型。

返回值:该模板返回一个布尔值,如下所示:

  • True :如果类型是const限定的。
  • False :如果类型是非const限定的。

下面的程序说明了C++ STL中的is_const模板:

程序1

// C++ program to illustrate
// is_const
  
#include 
#include 
using namespace std;
  
// main program
int main()
{
    cout << boolalpha;
    cout << "is_const:"
         << endl;
    cout << "int:"
         << is_const::value
         << '\n';
    cout << "const int:"
         << is_const::value
         << '\n';
    cout << "const int&:"
         << is_const::type>::value
         << '\n';
    return 0;
}
输出:
is_const:
int:false
const int:true
const int&:true

程序2

// C++ program to illustrate
// is_const
  
#include 
#include 
using namespace std;
  
// main program
int main()
{
    cout << boolalpha;
    cout << "is_const:"
         << endl;
    cout << "const int*:"
         << is_const::value
         << '\n';
    cout << "int* const:"
         << is_const::value
         << '\n';
    cout << "const int&:"
         << is_const::value
         << '\n';
  
    return 0;
}
输出:
is_const:
const int*:false
int* const:true
const int&:false

程序3

// C++ program to illustrate
// is_const
  
#include 
#include 
using namespace std;
  
// main program
int main()
{
    cout << boolalpha;
    cout << "is_const:"
         << endl;
    cout << "float:"
         << is_const::value
         << '\n';
    cout << "const float:"
         << is_const::value
         << '\n';
    cout << "char const:"
         << is_const::value
         << '\n';
    cout << "double:"
         << is_const::value
         << '\n';
  
    return 0;
}
输出:
is_const:
float:false
const float:true
char const:true
double:false
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”