<type_traits>頭文件中提供了C++ STL的std::add_const模板。 C++ STL的std::add_const模板用於獲取具有const限定符的T類型。 std::is_volatile用於檢查類型T是否為const限定。
頭文件:
#include<type_traits>
模板類別:
template < class T > struct add_const;
用法:
std::add_const<T>::value
參數:模板std::add_const接受單個參數T(Trait類),該參數用於將類型T聲明為const限定。
下麵是演示C++中std::add_const:的程序:
程序:
// C++ program to illustrate
// std::add_const
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
// Driver Code
int main()
{
// Declare variable of type
// int, const int, const int*,
// int * const and const int&
typedef add_const<int>::type A;
typedef add_const<const int>::type B;
typedef add_const<const int*>::type C;
typedef add_const<int* const>::type D;
typedef add_const<const int&>::type E;
cout << std::boolalpha;
// Checking const of the above
// declared variables
cout << "A is const int? "
<< is_same<const int, A>::value
<< endl;
cout << "B is const int? "
<< is_same<const int, B>::value
<< endl;
cout << "C is const int? "
<< is_same<const int, C>::value
<< endl;
cout << "D is const int? "
<< is_same<const int, D>::value
<< endl;
std::cout << "E is const int? "
<< is_same<const int, E>::value
<< endl;
return 0;
}
輸出:
A is const int? true B is const int? true C is const int? false D is const int? false E is const int? false
參考: http://www.cplusplus.com/reference/type_traits/add_const/
相關用法
- 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::add_const in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。