當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ std::add_cv用法及代碼示例

<type_traits>頭文件中存在C++ STL的std::add_cv模板。 C++ STL的std::add_cv模板用於獲取具有const和volatile限定的T類型。函數std::is_same::value用於檢查T是否為const和volatile限定。

頭文件:

#include<type_traits>

模板類別:

template< class T >
struct add_cv

用法:

std::add_const::type

參數:模板std::add_cv接受單個參數T(Trait類),以檢查T是否為const和volatile限定。



下麵是在C++中演示std::add_cv的程序:

程序:

// C++ program to illustrate 
// std::add_cv 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Driver Code 
int main() 
{ 
  
    // Declare variables of int, const, 
    // volatile, const volatile using add_cv 
    typedef add_cv<int>::type A; 
    typedef add_cv<const int>::type B; 
    typedef add_cv<volatile int>::type C; 
    typedef add_cv<const volatile int>::type D; 
  
    cout << boolalpha; 
  
    // Check the type of variable declared 
    // above is volatile or not 
  
    cout << "A:"
         << is_volatile<A>::value 
         << endl; 
  
    cout << "B:"
         << is_volatile<B>::value 
         << endl; 
  
    cout << "C:"
         << is_volatile<C>::value 
         << endl; 
  
    cout 
        << "D:"
        << is_volatile<D>::value 
        << endl; 
  
    return 0; 
}
輸出:
typedefs of const volatile int:
A:true
B:true
C:true
D:true

參考: http://www.cplusplus.com/reference/type_traits/add_cv/




相關用法


注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 std::add_cv in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。