在本文中,我們將討論 C++ STL 中 std::is_const 模板的工作、語法和示例。
C++ 中的 is_const 模板用於檢查定義的類型是否為 const-qualified 類型。
什麽是 const-qualified 類型?
當類型的值是常量時,我們稱該類型為 const-qualified。常量數據類型是一種類型,一旦在 const 中初始化了一個值,就不能在整個程序中更改或更改。
用法
template <class T> is_const;
參數
模板隻能有類型 T 的參數,並檢查給定的類型是否為常量限定符
返回值
它返回一個布爾值,如果給定類型是 const-qualifier,則返回 true,如果給定類型不是 const-qualifier,則返回 false。
示例
Input:is_const<const int>::value; Output:True Input:is_const<int>::value; Output:False
示例
#include <iostream>
#include <type_traits>
using namespace std;
int main() {
cout << boolalpha;
cout << "checking for is_const template:";
cout << "\nInt:"<<is_const<int>::value;
cout << "\nConst int:"<< is_const<const int>::value;
cout << "\nConst int&:"<< is_const<const int&>::value;
return 0;
}
輸出
如果我們運行上麵的代碼,它將生成以下輸出 -
checking for is_const template: Int:false Const int:true Const int&:false
示例
#include <iostream>
#include <type_traits>
using namespace std;
int main() {
cout << boolalpha;
cout << "checking for is_const template:";
cout << "\nFloat:"<<is_const<float>::value;
cout << "\nChar:"<<is_const<char>::value;
cout << "\nFloat *:"<<is_const<float*>::value;
cout << "\nChar *:"<<is_const<char*>::value;
cout << "\nConst int*:"<< is_const<const int*>::value;
cout << "\nint* const:"<< is_const<int* const>::value;
return 0;
}
輸出
如果我們運行上麵的代碼,它將生成以下輸出 -
checking for is_const template: Float:false Char:false Float *:false Char *:fakse Const int*:false int* const:true
相關用法
- C++ is_class用法及代碼示例
- C++ is_unsigned用法及代碼示例
- C++ is_fundamental用法及代碼示例
- C++ is_scalar用法及代碼示例
- C++ is_pointer用法及代碼示例
- C++ is_polymorphic用法及代碼示例
- C++ is_rvalue_reference用法及代碼示例
- C++ is_trivial用法及代碼示例
- C++ is_void用法及代碼示例
- C++ is_empty用法及代碼示例
- C++ is_final用法及代碼示例
- C++ is_signed用法及代碼示例
- C++ is_reference用法及代碼示例
- C++ is_pod用法及代碼示例
- C++ is_permutation()用法及代碼示例
- C++ is_abstract用法及代碼示例
- C++ is_arithmetic用法及代碼示例
- C++ is_standard_layout用法及代碼示例
- C++ is_lvalue_reference用法及代碼示例
- C++ isdigit()用法及代碼示例
注:本文由純淨天空篩選整理自Sunidhi Bansal大神的英文原創作品 is_const Template in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。